article

A template engine (or template parser or template processor) is software that processes input text (the template) to produce one or more output texts on a website.

Content on websites needs updating and standardization. A news website, for example, needs daily updating and each updated news item will be contextualized by a standard layout and standard page-location. A strategy to automate this standardization is:

  1. conceive the website with a template engine;
  2. specify the standards through a web templates;
  3. specify and update content on a database;
  4. incorporate each item of updated content, in a standard format, into the a web template with the template engine.

There are so many template engine examples, and Smarty, vlibTemplate, Velocity, FreeMarker, StringTemplate are some of them. To reduce the diversity, for "simplify" the life of companies and programers, there are a important class of "complex template engines", the XSLT parsers. It is a W3C effort to reduce the diversity of the engine implementatiom methods and template languages.

How the engine works


The processing itself generally involves replacing specific sequences of text with data provided by a model or resulting from more complex operations. E.g. a Smarty template often contains HTML and variables. It is static text. When Smarty (using PHP) is processing the template, all variables are replaced with assigned content. This content often is stored in a database. Some template engines like vlibTemplate or TinyButStrong have methods to fetch this content and use it with the template.

Some template engines (e.g. JSP) allow complete blocks of code to be embedded into a template, which is powerful, but gets very opaque as soon as a certain complexity level is reached. Smarty allows regular expressions and complex loop structures within the template, too. On the other hand some web developers prefer to handle logic within PHP and not within the template.

Template engines can be used for advanced modular programming, too. E.g. if you want to create a complex website, a template engine will help you to create clean and structured code.

Benefits of using template engines


  • separates code (PHP, Perl, ASP, etc.) from HTML. To understand why this is crucial, read Enforcing Strict Model-View Separation in Template Engines
  • when you get used to using template engines, your applications will be developed faster
  • your source code will be more organized, more clean and easier to understand
  • teamwork is a lot easier. Programmers can edit their code and artists or HTML specialists can create beautiful templates and layouts

Short code example


To use the Smarty template engine you have to use two files:
  • template.htm
  • php_script.php
Your template should look like this:


   

Hello {$X}

and your PHP script like that:

$tmpl->assign('X', 'World');
Or, when usign in a database context (figure, where $i denote the web page identification):

$data $data[02='World';
$tmpl->assign('X', $data*);

Not every template engine uses methods like "assign". Possible names are "setvar", "merge" and so on. See vlibTemplate and TinyButStrong for more detailed examples.

See also


External links


Web server software | Scripting languages | Template engines

Template Engine

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Template engine".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld