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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package templates

Templates

An HTML template compiler and renderer for PHP 8.1+.

License Version PHP CI PHPStan

The CI badge tracks GitHub Actions runs on the Toropyga/Templates repository 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 that phpstan.neon (also included) analyses src/ at level: max with zero errors as of this package's build, not a live, continuously re-checked status — re-run composer stan after any change to confirm it still holds.

Contents

Requirements

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:

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:

  1. Make sure the project runs on PHP ^8.1 and has the DOM extension enabled.
  2. Update the package with Composer and review the configured template, style and cache directories.
  3. Search templates for tmplphp and tmpltag. They are disabled by default in version 6.0.0. Enable them explicitly only when every affected template is trusted application code:

  4. 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.
  5. Add handling for Toropyga\TemplateException. The old debug-mode exit behavior has been replaced with exceptions. A temporary non-throwing mode is available through $templates->setThrowExceptions(false).
  6. If the application relies on $_SESSION['style'], explicitly enable $templates->use_session = true and ensure sessions are available.
  7. Review template filenames and includes. Absolute paths, .. traversal, symlink escapes and unreadable files are rejected.
  8. Clear the compiled-template cache after upgrading:

  9. 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

PHP Build Version
Package Version
Requires php Version ^8.1
ext-dom Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package toropyga/templates contains the following files

Loading the files please wait ...