free as in freedom template engine for php
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
source
test
.gitignore
.scrutinizer.yml
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

Template Engine for PHP

This project aims to deliver an easy to use, free as in freedom and fast template engine for php (code name: yepte - yet another php template engine).

The build status of the current master branch is tracked by Travis CI: Build Status Latest stable

The scrutinizer status are: code quality

The versioneye status is: Dependency Status

Take a look on openhub.net.

Why

I wanted to create a lean (in matter of lines of code and number of files) fast, extendable but expendable template engine for php. This project does not aim to shake on the throne of the big template engine available for php. They have different goals, more manpower and different ideas behind.

Personally, I like the php-text-template but sebastian has a different approach in mind (writing something to a file). Adding my goals to his project would add more complexity to his library.

Available Templates

Currently, this component tries to solve three problems when dealing with php templates. All Templates are stackable, meaning you can assign one template key with another template instance.

RuntimeContentBasedTemplate solve the problem to replacing content stored in a string.

FileBasedTemplate solves the problem replacing content stored in a file.

ComplexFileBasedTemplate solves the problem replacing complex content stored in a file. This is commonly known as the view in php frameworks.

CallableComplexFileBasedTemplateManager solves the problem externalise reusable template tasks. This is commonly known as the view helper pattern.

Notes

What is a complex content?

Complex content contains code like:

$isFoo = ($bar === 'foo');
if ($isFoo) {
     /* ... */ 
} else { 
    /*  ... something else */ 
}

What is a callable?

Callable is an other word for the famous view helper design pattern. The template provides a method called "registerCallable" to register a callable and bind it to a name.

//assuming the file in the relative path 'template.phtml' has the following content
//<?php $this->foobar('foo', 'bar');
$myViewHelper = function($foo, $bar) {
    return 'there is no ' . $foo . ' without a ' . $bar;
}
$template = new CallableComplexFileBasedTemplateManager('template.phtml');
$template->registerCallable('foobar', $myViewHelper);
echo $template->render() . PHP_EOL;
//expected result: there is no foo without a bar

What kind of complex content should I use?

Well, it is up to you and the code is pretty flexible.

My two cents are, limit yourself to foreach. if/else is one step further to "adding business logic to the template". switch is another step into this direction.

Usage

use Net\Bazzline\Component\Template\RuntimeContentBasedTemplate;

//create a instance
$template = new RuntimeContentBasedTemplate();

//set content
$template->setContent('there is no {one} without a {two}');

//assign variable one by one ...
$template->assignOne('one', 'foo');
//... or by providing an array
$template->assignMany(array('one' => 'foo'));
//you can also assign a template to a template
//  this is used if a layout template is defined with a {content} key for e.g.
$template->assignOne('content', $otherTemplate);

//you can render it in different ways
//1) explicit calling the method
echo $template->render();
//2) casting it to a string
echo (string) $template;
//3) using it as a function
//  you can also provide all optional parameters like in the constructor
echo $template();

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_template
cd vendor/net_bazzline/php_component_template
git clone https://github.com/bazzline/php_component_template .

With Packagist

composer require net_bazzline/php_component_template:dev-master

API

API is available at bazzline.net.

History

  • upcomming
    • @todo
      • add examples
      • add download per months icon
      • add refuse/take/resign if needed and useful
      • add unit tests
      • implement caching
      • add support for different template extensions
      • refactor internals and replace array data with array of immutable domain objects
    • moved test autoloading into fitting configuration
    • moved zend-expressive-template adapter code into own repository
    • removed TryToInstallZendExpressiveTemplate command
    • removed zend-expressive-template as suggested package
    • restructured composer.json
  • 3.1.3 - released at 31.01.2016
    • added zend-expressive-template as suggested package
    • added TryToInstallZendExpressiveTemplate command that installs zend-expressive-template if requirements are met in development mode
    • added building for php 7
    • added test for stacking template into a template
    • moved to psr-4 auto loading
    • removed building for php 5.3.3
  • 3.1.2 - released at 26.01.2016
    • updated dependencies
  • 3.1.1 - released at 11.12.2015
    • refactored CallableComplexFileBasedTemplateManager::registerCallable()
    • updated dependencies
  • 3.1.0 - released at 28.10.2015
  • 3.0.0 - released at 09.10.2015
    • added links to travis, scrutinizer, openhub, versioneye
    • added the DelimiterInterface
    • implemented __invoke in all templates to speed up usage and rendering
    • implemented reset
    • refactored existing templates by introducing AbstractFileBasedTemplate and slicing out the delimiter handling
    • renamed setOpenDelimiter to setOpeningDelimiter
    • renamed FileTemplate to FileBasedTemplate
    • renamed StringTemplate to RuntimeContentBasedTemplate
    • renamed ViewTemplate to ComplexFileBasedTemplate
    • shifted order from filePath and variables in the constructor of RuntimeContentBasedTemplate and FileBasedContent
    • started link section
    • updated ComplexFileBasedTemplate variable handling by adding "EXTR_SKIP" to the extract method
    • updated dependency
  • 2.1.0 - released at 06.10.2015
    • added throws RuntimeException to __toString() method description
    • added ViewTemplate
    • add isAssigned
  • 2.0.0 - released at 02.10.2015
    • added TemplateInterface
    • decoupled file based template from generic template
      • renamed class Template to AbstractTemplate
      • introduced FileTemplate and StringTemplate
  • 1.1.0 - released at 01.10.2015
    • fixed major bug in "assignOne" (now it is working as expected)
  • 1.0.0 - released at 01.10.2015
    • initial release

Links to other libraries

Final Words

Star it if you like it :-). Add issues if you need it. Pull patches if you enjoy it. Write a blog entry if you use it :-D.