Download the PHP package hengeb/simplates without Composer
On this page you can find all versions of the php package hengeb/simplates. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hengeb/simplates
More information about hengeb/simplates
Files in hengeb/simplates
Package simplates
Short Description Template Engine for lightweight templates with automatic escaping
License MIT
Informations about the package simplates
simplates
Simplates as a template engine for PHP for lightweight templates with automatic escaping
basic usage
Your PHP file:
And in /path/to/your/templates/user/profile.php
(or /path/to/your/templates/user/profile.tpl.php
if you prefer) put:
This will be rendered like this:
The strings are escaped automatically but object methods and properties can be accessed.
You can use the raw method of a variable to access the raw data:
no escaping in text output
If, in the example above, you had saved your template as /path/to/your/templates/user/profile.txt.php
(note the .txt
in the file name), then no auto-escaping takes place but you can still use all the other features of template variables.
advanced auto-escaping feature
Object properties and return values will be auto-escaped as well. Variables can be arrays as well.
A more advaned example:
And in your template:
This will render like this (white space has been modified in the result):
Dates and times
and in your template now.tpl.php
:
result:
The date is converted to the provided time zone (variable named _timeZone
). If you do not provide a time zone, the time zone will not be changed.
Another way is to provide the timezone name in the call of the format
method:
global variables
In the examples above all the variables are passed in the context of the template. If a variable is used in many places you can set a global template variable like this:
Reusing templates
You can use the extends
and include
methods to reuse templates:
In your template, use:
And in layout.php
put:
And in partials/alert.php
put:
Result:
return values
You can return values from your template content to the calling context.
For example, if you have a template for an email you might want to use your template to generate the subject and return it:
In your template mail/confirm.txt.php
:
And in the calling context:
falsish values
The auto-escaping feature comes with the drawback that falsish variables cannot be checked with if ($value)
or if (!empty($value))
because in PHP, objects are always truish and technically, all the template variables are object (of the TemplateVariable
class).
Use the engine's check
, the variable's isTrue
or isEmpty
methods or other means instead:
instead of:
use
the suggested means are:
built-in methods of template variables
Template variables come with some handy methods that make writing templates cleaner. Use them like this: <?=$data->json()?>
escape()
: escape HTML special characters (only needed in non-auto-escaping contexts)raw()
: do not escape (only needed in auto-escaping contexts)json(bool $pretty = false)
: JSON representation of the data. Use like this:htmlTable()
: return HTML table of the array contenthtmlList(string $listType = 'ol')
: return HTML list of array contentsdump()
: wrap value in<pre>
tagimplode(string $separator = ', ')
: implode array listinputHidden(string $name = '')
: create HTML input element of type=hiddeninput(...$attributes)
: create HTML input element of type=text (default) or similar types like type=date (call like this:$date->input(type: 'date')
), you can also add a label with alable
attribute.box(...$attributes)
: create HTML input element of type=checkbox (default) or type=radio, you can also add a label with alable
attribute.textarea(...$attributes)
: create HTML textarea element.select(array $options, ...$attributes)
: create HTML select element. The$options
parameter is an array like[value => label, ...]
Note that methods of the actual object that is stored in the template variable are a bit harder to access if their name coincidentally matches one of the built-in methods of the template variable proxy class. You would have to use the __call
method in this case: <?=$variable->__call('input', [$arguments])?>
extend the functionality
You can extend the TemplateVariable
class and add your own methods. You have to extend the Engine
class as well so it uses your class:
With this extended template engine, every template variable has a bold() method:
This will render like this:
Of course, you can also extend the Engine
class itself to provide extra features.