To describe this very powerful and at the same time laconic template engine, I simply copy the text from the mana.
«PHPTAL system for PHP. PHPTAL supports TAL, METAL, I18N namespaces ”and“ PHPTALES ”is It defines how XML attribute values are handled »
Offered under the LGPL license here
http://phptal.org/ .
I have been making templates for PHPTAL for about a year now and consider it “enchanting” :). There are a couple of my patches in the code, so I know the subject from the inside.
')
Next, I will make a review article from which you will understand for sure that I am not a writer and why I strongly opposed the requests of the authors to “reveal the topic” and I hope at least a little bit to popularize this masterpiece.
XML syntax
Templates TAL, and PHPTAL, respectively, are also XML documents, cruel and real, and not “where the angle brackets” are.
Here you have both entities and CDATA sections and, do not believe, an XML declaration.
What is good?
It disciplines - you will never be left with uncovered tags because of which “suddenly” layout will go, template maker simply will not miss such an outrage.
Probably there is no code editor that does not understand the XML format.
Your coder is not a schoolboy.
What is bad?
Your coder is not a schoolboy, yes, I remember that it was in the pros, but now for 10 bucks you do not impose signs in front page
The IE hack implementation can be offensive (at the end one of the examples)
Inline-JS is better designed as a CDATA section, well, or done “for an adult” in separate js files.
Some people will have to read a book about XML, not sure what is bad.
Attributes
All the power of TAL is hidden in the attributes, to describe exactly 1 (one) element in the specification, and that, as stated in the speculation “is syntactic sugar”, and you can completely do without it. Therefore we speak TAL we mean attributes.
Than this is good - exactly to everyone, when you get a layout from XHTML layout maker, it is already a TAL template, then it will only be iteratively “pull” it, it is in TAL that “pull the pattern” very accurately describes the process.
In the above-mentioned spec, PHPTAL describes as many as 18 service attributes, of which you will never use a good half.
Then I’ll go over very briefly and really important ones - the descriptions will be given by the code:
tal: define, tal: content
<div tal: define = "global title obj / getTitle; content obj / getContent">
<div class = "post_title" tal: content = "title"> Lorem ipsum </ div>
<div class = "post_content" tal: content = "content"> Lorem ipsum </ div>
</ div>
Normal constants have a scope limited by the tag in which they are defined, this feature is similar to xslt and avoids intersection by name.
Global constants act on the entire processing flow of a template, I write a stream and not a template, since templates can be inherited and then when processing one, the chain of templates is actually processed.
An example of when global constants strongly “deliver” is at the end of a topic.
tal: condition, tal: repeat, tal: attributes, i18n: translate
<div tal: repeat = "post posts" tal: attributes = "class php: repeat.post.odd? 'odd': NULL">
<div class = "post_title" tal: content = "post / getTitle"> Lorem ipsum </ div>
<div class = "post_content" tal: content = "post / getContent"> Lorem ipsum </ div>
<a class="post_cut" tal:condition="post/hasMore" i18n:translate=""> Read more </a>
</ div>
Here is a list of topics with op-based links to “more” and zebra.
The topic of zebra is revealed in the official mana
phptal.org/manual/ru/split/tal-attributes.htmlWhen the template engine is fully linked, in this template the text “Read more” will be translated by the translator (gettext by default)
metal: define-macro, metal: use-macro, metal: define-slot, metal: fill-slot
These 4 attributes implement template inheritance, then we work with the home.html template that inherits the basic layout common to all:
home.html
<? xml version = "1.0"?>
<tal: block metal: fill-slot = "custom-js">
<script rel = "stylesheet" src = "/ mootools-1.2-fx.js" type = "text / javascript" />
</ tal: block>
<tal: block metal: fill-slot = "custom-css">
<style type = "text / css" media = "all">
@import url (<tal: block tal: content = "/ main.css" />);
</ style>
</ tal: block>
<tal: block metal: use-macro = "layout / page">
<body metal: fill-slot = "body" tal: define = "global title post / getTitle">
<div tal: content = "post / getContent"> Post content text </ div>
</ body>
</ tal: block>
layout.html
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Strict // EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html metal: define-macro = "page" xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title tal: content = "title | default"> PHPTAL global title example </ title>
<tal: block metal: define-slot = "meta">
<meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8" />
<meta name = "generator" content = "velocity framework" tal: attributes = "content generator | default" />
<meta name = "description" tal: condition = "exists: description" content = "$ {description}" />
<meta name = "keywords" tal: condition = "exists: keywords" content = "$ {keywords}" />
</ tal: block>
<script rel = "stylesheet" src = "/ mootools-1.2-core.js" type = "text / javascript" />
<tal: block metal: define-slot = "custom-js" />
<style type = "text / css" media = "all">
@import url (<tal: block tal: content = "/ main.css" />);
</ style>
<tal: block metal: define-slot = "custom-css" />
</ head>
<body metal: define-slot = "body"> Lorem ipsum </ body>
</ html>
Still
The 10 attributes described are enough to get you started, the remaining 8 are well described in man
Teyla
As you can see above, expressions are written in a special format, a common expression format:
prefix:
, if the prefix is not defined it is considered equal to "path"
In PHPTAL, 5 types of expressions are defined (path, php, string, not, exists), in the original TAL php is replaced with python.
Each type of tails, namely, the so-called expressions, defines the format, everything is well described in mana, I’ll dwell only on the base path.
The tail path is made very similar to the XPath syntax, and it will be very convenient to those familiar with it, as the expression:
obj/getObject2/path
equivalent to
$obj->getObject2()->path;
.
The path analyzer automatically tries to call the corresponding methods, members and keys of arrays in order of priority from mana.
PHPTAL presumes that the developer will finish writing the emails he needs, thereby expanding the functionality.
Tricks and examples
Global constants
Global constants are very convenient, the most typical example is the title of the page. Now you can define it anywhere.
layout.html
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Strict // EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html metal: define-macro = "page" xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title tal: content = "title | default"> PHPTAL global title example </ title>
<tal: block metal: define-slot = "meta">
<meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8" />
<meta name = "generator" content = "velocity framework" tal: attributes = "content generator | default" />
<meta name = "description" tal: condition = "exists: description" content = "$ {description}" />
<meta name = "keywords" tal: condition = "exists: keywords" content = "$ {keywords}" />
</ tal: block>
</ head>
<body metal: define-slot = "body">
</ html>
home.html
<? xml version = "1.0"?>
<tal: block metal: use-macro = "layout / page">
<body metal: fill-slot = "body" tal: define = "global title post / getTitle">
Page body
</ body>
</ tal: block>
In the specified example, the home.html template is used for output, and the layout.html for a long time is used for uniform border, but even in this case you can control it, in particular, dynamically output the header, for example, by the name of the blog post from the database
Inherited Output of Connected Resources
This example somewhat echoes the previous one, but is implemented differently, for example, you need to be able to add resources in the inherited template (css js to the head section of the layout):
layout.html
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Strict // EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html metal: define-macro = "page" xmlns = "http://www.w3.org/1999/xhtml">
<head>
<script rel = "stylesheet" src = "/ mootools-1.2-core.js" type = "text / javascript" />
<tal: block metal: define-slot = "custom-js" />
<style type = "text / css" media = "all">
@import url (<tal: block tal: content = "/ main.css" />);
</ style>
<tal: block metal: define-slot = "custom-css" />
</ head>
<body metal: define-slot = "body">
</ html>
home.html
<? xml version = "1.0"?>
<tal: block metal: use-macro = "layout / page">
<tal: block metal: fill-slot = "custom-js">
<script rel = "stylesheet" src = "/ mootools-1.2-fx.js" type = "text / javascript" />
</ tal: block>
<tal: block metal: fill-slot = "custom-css">
<style type = "text / css" media = "all">
@import url (<tal: block tal: content = "/ main.css" />);
</ style>
</ tal: block>
<body metal: fill-slot = "body">
Page body
</ body>
</ tal: block>
Inline js
<script type = "text / javascript">
// <! [CDATA [
var $ var = $ {json: var};
// since this is a CDATA, you can use the angle bracket
if ($ var <1) {
// bla .... bla
}
//]]>
</ script>
Here is an example of how to write JS without fear of official characters.
json: this is my samopisny teyl which maput variable in the JS code :)
Documentation
It is not always convenient to use the online version. Along with the sources of the template engine, the translated percent is delivered to the 50 docbook book, all you have to do is to convert it into a convenient format. Using the tools available here
http://docbook.sourceforge.net/ you can even get chm, and with a certain dexterity and free time and pdf.
Performance
PHPTAL, like smarti and many others, generates PHP-runtime code and already works with it, the code is very high quality and not redundant due to this the speed is very, very good -
http://fabien.potencier.org/article/34/templating-engines-in-php