Download the PHP package echo-fusion/templatemanager without Composer
On this page you can find all versions of the php package echo-fusion/templatemanager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download echo-fusion/templatemanager
More information about echo-fusion/templatemanager
Files in echo-fusion/templatemanager
Package templatemanager
Short Description Framework-independent template manager for EchoFusion: a built-in Twig/Smarty-style template language, plain PHP templates, and pluggable engines such as Twig through configuration.
License MIT
Homepage https://github.com/echo-fusion/templatemanager
Informations about the package templatemanager
TemplateManager
TemplateManager renders templates for the EchoFusion framework. It has no dependency on Symfony or Laravel: it ships its own small template language (in the spirit of Twig/Smarty), supports plain PHP templates, and lets you plug in other engines such as Twig through configuration.
It implements EchoFusion\Contracts\TemplateManager\TemplateManagerInterface from echo-fusion/contracts, so other packages can depend on the interface only.
Part of EchoFusion, a modular PHP framework built from small packages.
Installation
Requirements
- PHP 8.1, 8.2 or 8.3
- ext-mbstring
Quick start
With no engines setting the built-in tpl engine and plain php engine are enabled. render() returns a string; wrap it yourself in a response, e.g. new HtmlResponse($templates->render('home')) from echo-fusion/responsemanager.
The built-in template language (.tpl)
- Variables:
a.b.creads array keys,ArrayAccess, public properties, and the gettersgetB(),isB(),hasB(). Missing values print nothing. - Expressions: strings, numbers,
true/false/null,[lists],+ - * / %,~(concatenate),== != < > <= >= === !==,and/or/not,?:and??. Function calls are not allowed; use filters. - Loops:
loop.index,loop.index0,loop.first,loop.last,loop.length. An{% else %}runs when there is nothing to loop over. - Filters:
upper lower capitalize title trim length default join keys first last reverse date json number truncate replace url_encode raw escape. Chain them:{{ name|trim|upper }}. - Auto-escaping is on. Turn it off with the
autoescapeoption, or force it with|e.
Layouts: extends and block
Inheritance can be nested. In a child template only {% block %} and {% set %} may appear outside blocks.
Custom filters
Compilation and cache
Templates are compiled to PHP files. Options for the tpl engine:
| Option | Default | |
|---|---|---|
cache |
<system temp>/echofusion-templates |
cache directory |
auto_reload |
true |
recompile when the template changes; set false in production for the fastest start |
autoescape |
true |
HTML-escape {{ }} |
Expressions are translated token by token, so a template cannot inject PHP code.
Plain PHP templates (.php)
In a layout, <?= $this->section('content') ?> is the child output and $this->section('sidebar', 'default') any named section. $this->e() escapes. Section output is already rendered, so it is not escaped again.
Namespaces (modules)
Give a directory a namespace and address its templates as @namespace/name:
This pairs with echo-fusion/modulemanager:
Directories are searched in the order they were added, so add an application directory first to override a module template.
Configuration
An engine entry is the file extension. tpl, php and twig are built in; for any other extension give a class. Names can carry the extension (render('home.twig')) to pick an engine explicitly. Pass the same array to TemplateManager::fromConfig(), or to $manager->configure() on an existing manager.
Your own engine
Implement EchoFusion\Contracts\TemplateManager\TemplateEngineInterface (render(), exists()). The manager creates it as new YourEngine(TemplateFinder $finder, string $extension, array $options); use $finder->find($name, $extension) to locate files and namespaces.
Twig
TwigEngine hands the manager's directories and namespaces to Twig, so @blog/post works the same way. options go to Twig\Environment. Twig is optional: without twig/twig installed, only configuring a twig engine fails.
Service provider
EchoFusion\TemplateManager\Providers\TemplateManagerProvider registers TemplateManagerInterface and TemplateEngineInterface in a container and, on boot(), configures the manager from the templates section of your config.
Errors
All exceptions extend EchoFusion\TemplateManager\Exceptions\TemplateException: TemplateNotFoundException, TemplateSyntaxException (with template name and line), InvalidConfigException.
Template names are validated: .., absolute paths, backslashes and NUL bytes are never resolved, so a user-controlled name cannot read files outside the template directories.
Testing
Credits
License
The MIT License (MIT). Please see LICENSE for more information.
All versions of templatemanager with dependencies
echo-fusion/contracts Version ^1.6 || ^2.0
ext-mbstring Version *