📜 ⬆️ ⬇️

PHPTAL Template Engine

This template engine has an approach that differs from other types of template engines. And it can not but interest.
At the request of PHPTAL, Habr provides only one link ,
TAL is the Attributive Pattern Language. It is called that because language commands are attributes of tags.

Native template logic

In native templates, PHP or some other language is taken as a basis, and HTML is obtained as a result of its work.
  1. < table > <? foreach ($ myarray as $ myitem ) : ? > < tr >< td ><? =$myitem? ></ td > <? endforeach ;? > </ table >
  2. < table > <? foreach ($ myarray as $ myitem ) : ? > < tr >< td ><? =$myitem? ></ td > <? endforeach ;? > </ table >
  3. < table > <? foreach ($ myarray as $ myitem ) : ? > < tr >< td ><? =$myitem? ></ td > <? endforeach ;? > </ table >
  4. < table > <? foreach ($ myarray as $ myitem ) : ? > < tr >< td ><? =$myitem? ></ td > <? endforeach ;? > </ table >
  5. < table > <? foreach ($ myarray as $ myitem ) : ? > < tr >< td ><? =$myitem? ></ td > <? endforeach ;? > </ table >
HTML is lost somewhere in the wilds of the language. This is all badly readable. May produce difficult to detect errors. For example such
  1. < div > <? php if ($ flag ):? > text </ div > <? endif ; >
By the way, because Smarti was so widely spread that it does not change the logic of PHP, but simply simplifies the syntax a little.

TAL logic

Here is the same example loop written in PHPTAL
  1. < table >
  2. < tr tal: repeat = "myitem myarray" >
  3. < td tal: content = "myitem" > Text to be replaced by the value of myitem </ td >
  4. < td tal: replace = "" > Example 1 </ td >
  5. < td tal: replace = "" > Example 2 </ td >
  6. < td tal: replace = "" > Example 3 </ td >
  7. </ tr >
  8. </ table >
* This source code was highlighted with Source Code Highlighter .
Its advantages:With this approach, you work with HTML tags. You indicate which tag should repeat several times in a loop, and which tag should display information.
TAL Namespace
In the order they are processed by the template engine:
  1. tal: define - This attribute declares one or more variables that can then be used in a template.
  2. tal: condition - The tag and its contents will be displayed only when the condition is met.
  3. tal: repeat - This attribute is used to loop over the enumerated data.
  4. tal: replace - Using this attribute, you can replace the entire element (including the opening and closing tags) with the specified string, or empty if the value is not specified.
  5. tal: content - The value of this attribute will become the text inside the tag.
  6. tal: attributes - With tal: attributes, you can set tag attribute values ​​on the fly.
  7. tal: omit-tag - This attribute will cause the PHPTAL parser to ignore the element's opening and closing tags and display only its contents.
METAL namespace (macros)
At their core, macros are regular templates that can be repeatedly inserted into other templates.
  1. metal: define-macro - This attribute declares a macro
  2. metal: use-macro - This attribute causes the macro and includes the result of its execution in the current template in the place where it was called
  3. metal: define-slot - This attribute must be declared inside a tag that declares a macro (metal: define-macro).
  4. metal: fill-slot - This attribute can only be used inside an element with metal: use-macro.
Macros - based on them, include and inheritance is implemented
PHPTALES
Expression syntax allows, for example, to write php code inside attributes
tal: content = "php: strtolower ($ value)"
  1. path: - is used by default. example: tal: content = "data / user / name"
  2. string: - the value is parsed as a string. example: tal: replace = "string: this is a $$ 100 page"
  3. not: - used only in tal: condition
  4. exists: - analog php isset ()
  5. php: - php syntax

Namespace I18N (internalization)
A tool for creating multilingual templates
  1. i18n: translate - This attribute identifies the text to be translated using the translation system built into PHPTAL.
  2. i18n: attributes - Specifies which attributes should be translated.
  3. i18n: name - You can dynamically add variable values ​​to the translation itself.
Empowering PHPTAL
PHPTAL allows a programmer to add triggers , filters , phptales to it , and also simply transfer objects to it, which methods are then called in a template. This opens up huge opportunities for extending the template engine, as well as the possibility of embedding it into your framework.
Links to primary sources: official site | Russian translation of documentation

')

Source: https://habr.com/ru/post/75082/


All Articles