PHP Classes

PHP Template Engine Component: Process templates using template object results

Recommend this page to a friend!
  Info   View files Documentation   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 63 This week: 1All time: 10,388 This week: 560Up
Version License PHP version Categories
php_component_templa 1.0.0GNU Lesser Genera...5PHP 5, Templates
Description 

Author

This package can process templates using template object results.

It can process templates by assigning values to marks of several types.

Currently, it can:

- Assign one template mark to a given string value

- Assign many template marks to given string values passed as an array

- Assign template marks to the results of the processing of a template using another template engine object

Innovation Award
PHP Programming Innovation award nominee
May 2022
Number 2
PHP is often used to process templates that will define the presentation of Web pages.

In some cases, pages can present the content complexly that involves nested sections because they contain internal parts.

The package provides a template engine that can deal with this complexity.

It allows assigning template marks with content resulting from processing other templates by other objects of the template classes provided by this package.

This way, developers simplify complex template processing by splitting the processing between different objects that can process inner parts of the page processed using templates.

Manuel Lemos
Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 150195 in Germany Germany
Week rank: 109 Up6 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Documentation

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 removedTryToInstallZendExpressiveTemplate* 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 addedTryToInstallZendExpressiveTemplate* 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 * added CallableComplexFileBasedTemplateManager which allows registering callable to easy up reusing code and nest templates in templates * added "TemplateDumper" to easy up dumping rendered content to a file * easy up reading readme
  • 3.0.0 - released at 09.10.2015 * added links to travis, scrutinizer, openhub, versioneye added theDelimiterInterface* implemented__invoke* in all templates to speed up usage and rendering implementedreset* refactored existing templates by introducingAbstractFileBasedTemplate* and slicing out the delimiter handling renamedsetOpenDelimitertosetOpeningDelimiter* renamedFileTemplatetoFileBasedTemplate* renamedStringTemplatetoRuntimeContentBasedTemplate* renamedViewTemplatetoComplexFileBasedTemplate* shifted order fromfilePathandvariablesin the constructor ofRuntimeContentBasedTemplateandFileBasedContent* * started link section updatedComplexFileBasedTemplatevariable handling by adding "EXTR_SKIP" to theextract* method * updated dependency
  • 2.1.0 - released at 06.10.2015 addedthrows RuntimeExceptionto__toString()* method description addedViewTemplate* addisAssigned*
  • 2.0.0 - released at 02.10.2015 addedTemplateInterface* * decoupled file based template from generic template renamed classTemplatetoAbstractTemplate* introducedFileTemplateandStringTemplate*
  • 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.


  Files folder image Files  
File Role Description
Files folder imagesource (10 files)
Files folder imagetest (6 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  source  
File Role Description
  Plain text file AbstractFileBasedTemplate.php Class Class source
  Plain text file AbstractTemplate.php Class Class source
  Plain text file CallableComplexFil...TemplateManager.php Class Class source
  Plain text file ComplexFileBasedTemplate.php Class Class source
  Plain text file DelimiterInterface.php Class Class source
  Plain text file ExpressiveTemplateAdapter.php Class Class source
  Plain text file FileBasedTemplate.php Class Class source
  Plain text file RuntimeContentBasedTemplate.php Class Class source
  Plain text file TemplateDumper.php Class Class source
  Plain text file TemplateInterface.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file CallableComplexFil...lateManagerTest.php Class Class source
  Plain text file ComplexFileBasedTemplateTest.php Class Class source
  Plain text file ExpressiveTemplateAdapterTest.php Class Class source
  Plain text file FileBasedTemplateTest.php Class Class source
  Plain text file RuntimeContentBasedTemplateTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:63
This week:1
All time:10,388
This week:560Up