Download the PHP package toropyga/templates without Composer
On this page you can find all versions of the php package toropyga/templates. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download toropyga/templates
More information about toropyga/templates
Files in toropyga/templates
Package templates
Short Description A secure PHP 8.1+ template compiler and renderer
License MIT
Informations about the package templates
Templates
An HTML template compiler and renderer for PHP 8.1+.
The CI badge tracks GitHub Actions runs on the
Toropyga/Templatesrepository and only lights up once.github/workflows/ci.yml(included in this package) is pushed there and has run at least once. The PHPStan badge is static: it reflects thatphpstan.neon(also included) analysessrc/atlevel: maxwith zero errors as of this package's build, not a live, continuously re-checked status — re-runcomposer stanafter any change to confirm it still holds.
Contents
- Requirements
- Installation
- Quick Start
- Template Syntax
- Variables
- Includes
- Blocks
- PHP in Templates
- Errors
- Cache
- Migrating Legacy Projects
- Legacy Configuration
- Mapping Legacy Constants
- License
Requirements
- PHP
^8.1 - PHP
DOMextension (ext-dom)
Installation
Install the package with Composer:
Then load Composer's autoloader:
Quick Start
The recommended API uses an explicit configuration object:
With templates/default/hello.html:
The result is:
render() returns the rendered string. The compatibility method output()
can either print the template or return it when called with true:
Template Syntax
Variables
Use {$name} for a value and array notation for nested values:
Values are escaped for HTML by default. Use |raw only for trusted HTML:
You can change the escaping policy explicitly:
Includes
Include another template with:
Template paths are restricted to the configured style directory. Absolute
paths, .. traversal and symlink escapes are rejected.
Blocks
To render only a marked block:
PHP in Templates
Execution of PHP from tmplphp and tmpltag is disabled by default. This is
recommended for untrusted or user-editable templates. Legacy templates can
opt in explicitly:
Only enable this mode when all templates are trusted application code.
Errors
Errors are reported through Toropyga\TemplateException and its specialized
subclasses:
TemplatePathExceptionTemplateCompilationExceptionTemplateCacheException
The legacy non-throwing mode remains available during migration:
Cache
Compiled templates are written to the configured cache directory. Each cache entry has a manifest containing the compiler version, source path and SHA-256 source hash. The cache is invalidated when the source or compiler changes.
Clear the cache explicitly with:
Migrating Legacy Projects
Complete the following steps before upgrading an existing project:
- Make sure the project runs on PHP
^8.1and has theDOMextension enabled. - Update the package with Composer and review the configured template, style and cache directories.
-
Search templates for
tmplphpandtmpltag. They are disabled by default in version 6.0.0. Enable them explicitly only when every affected template is trusted application code: - Review every variable that is expected to contain HTML. Values are escaped
by default now. Mark only trusted values with
|raw, or temporarily call$templates->setEscapeHtml(false)during migration. - Add handling for
Toropyga\TemplateException. The old debug-modeexitbehavior has been replaced with exceptions. A temporary non-throwing mode is available through$templates->setThrowExceptions(false). - If the application relies on
$_SESSION['style'], explicitly enable$templates->use_session = trueand ensure sessions are available. - Review template filenames and includes. Absolute paths,
..traversal, symlink escapes and unreadable files are rejected. -
Clear the compiled-template cache after upgrading:
- Manually verify representative pages, especially pages using custom HTML, PHP template blocks, includes, sessions and raw HTML values.
The legacy constructor and method names remain available, but version 6.0.0 does not guarantee identical output for unsafe PHP templates, unescaped HTML, invalid paths or non-standard HTML markup.
Legacy Configuration
The constructor and legacy constants remain supported for existing projects:
Mapping Legacy Constants
Legacy constants can be passed explicitly to TemplateConfig:
The mapping is:
| Legacy constant | TemplateConfig property |
|---|---|
ROOT_PATH |
rootPath |
TMPL_DIR |
templatePath |
TMPL_STYLE |
style |
TMPL_CACHE |
cachePath |
SITE_PATH |
sitePath |
Templates::fromConfig() does not read global constants automatically. This
keeps modern instances isolated and allows different project roots in one
process. The legacy constructor continues to read the constants automatically.
The preferred long-term approach is TemplateConfig, because it avoids global
configuration, keeps the configured root and site path local to the instance,
and makes filesystem paths explicit. Modern instances can safely use different
roots in the same process.
Inline CSS resources are also rewritten by the DOM asset compiler:
Testing
The distributed package includes two dependency-free regression scripts (no PHPUnit required):
Both exit 0 and print OK (<n> assertions) on success, or exit 1 with
the first failing assertion on stderr.
Static analysis (PHPStan, level: max) is configured in phpstan.neon:
.github/workflows/ci.yml runs both the regression suite (across PHP 8.1,
8.2 and 8.3) and PHPStan on every push and pull request.
License
This project is licensed under the MIT License.
See CHANGELOG.md for release history.
All versions of templates with dependencies
ext-dom Version *