Download the PHP package hokoo/templater without Composer
On this page you can find all versions of the php package hokoo/templater. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hokoo/templater
More information about hokoo/templater
Files in hokoo/templater
Package templater
Short Description Keep your templates in one place apart from your php code.
License GPL-3.0-only
Informations about the package templater
Anatomy: a templater for PHP
HTML Templater for PHP.
Lightweight, simple but powerful templater for rendering HTML templates in PHP.
I believe that you'd like to
- keep your HTML clean and simple, and
- maintain the logic in PHP, not in HTML.
Table of Contents
- How rendering works
- Requirements
- Installation
- Getting Started
- Tags
- Predefined tags
- Blocks & Containers
- Detached mode
- Rendering contract and escaping
- Back compatibility
- Development roadmap
How rendering works
Inserted data is opaque and is never parsed as template source. The empty-data bypass and the different detached-block behavior are part of the public contract, not optimizations.
Requirements
PHP 8.0 and later.
Installation
Getting Started
Here are some examples of how to use the templater.
:a: Tags
Example:
PHP code:
The result will be:
Read more theory about Tags
Tag - a point in the template where you can insert a value. Tag should be considered as a placeholder for a value. In the Anatomy's paradigm, the 'tag' is only entity that can be replaced with a value. The tag is a string that starts with `{{` and ends with `}}`. Regular tags render raw values. Use `{{tag|e}}` for user-controlled HTML text or quoted HTML attribute values. Attribute placeholders must always be enclosed in quotes. See [Rendering contract and escaping](#rendering-contract-and-escaping). So, in the example above, the `{{title}}` and `{{content}}` are tags. Let's render the template with the values.:a: Predefined tags
Example:
PHP code:
The result will be:
Read more theory about Predefined Tags
Predefined tags are tags that can render only values predefined by the template. Predefined tag's modifier can only accept an integer value as index of one of the predefined values (starting from 0). Any invalid modifier value (non-integer or integer that points beyond of the array) will be considered as 0. A missing, negative, or out-of-range modifier is also considered as 0. The default values' delimiter is `|`. You can change it by setting the `delimiter` property of the tag.:a: Blocks & Containers
Example:
PHP code:
The result will be:
Read more theory about Blocks & Containers
Blocks are a way to have a component-like structure in the template. You can consider blocks as a template inside a template. You can define as many blocks as you want and render them in any order. Each block definition must have a unique name. The same definition can be rendered repeatedly and recursively through nested containers. You can put one block into another block. This is how you can create a nested structure without any restrictions on the depth of nesting.:a: Detached mode
Since we consider blocks as an almost independent template, you can use blocks out of context of the main template.
Suppose, you need to render the "article" block from the template above in a different place. Great, there's no need to duplicate the block's code. Just render the block separately.
The result would be:
Rendering contract and escaping
Calling render( $template, [] ) intentionally returns the original template
without replacing tags, extracting blocks, or validating its syntax. Detached
blocks are different: renderBlock() can render a hard-coded block with its
default empty data array.
{{tag}} remains raw for version 4 compatibility. Use {{tag|e}} for HTML
escaping:
HTML escaping is not a substitute for JavaScript, CSS, or URL-specific
encoding. In HTML attributes, always put an escaped placeholder inside quotes,
for example data-label="{{label|e}}"; never use an unquoted placeholder. The
complete value, error, block grammar, and security contracts are documented in
Anatomy template language.
Back compatibility
Version 3 compatibility is deprecated as of 4.2 and is planned for removal in version 5. It receives compatibility fixes during the version 4 lifecycle but does not receive new features. Syntax older than version 3 is not supported.
To use v3 syntax, you have to instantiate the legacy version of the templater:
Development roadmap
Version 4.2 is the current stable contract baseline. The next maintenance and major-version work, its dependencies, and the required decision gates are tracked in the development roadmap. Release history is kept in the changelog.