Download the PHP package devtheorem/php-handlebars without Composer

On this page you can find all versions of the php package devtheorem/php-handlebars. 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 php-handlebars

PHP Handlebars

A blazing fast, spec-compliant PHP implementation of Handlebars.

The syntax of Handlebars is generally a superset of Mustache, so in most cases it is possible to swap out Mustache for Handlebars and continue using the same templates.

Features

Performance

PHP Handlebars started as a fork of LightnCandy, but has been rewritten with an AST-based parser and optimized runtime to enable full Handlebars.js compatibility with better performance.

PHP Handlebars compiles and executes complex templates over 40% faster than LightnCandy, with 60% lower memory usage:

Library Compile time Runtime Total time Peak memory usage
LightnCandy 1.2.6 5.0 ms 2.4 ms 7.4 ms 5.3 MB
PHP Handlebars 2.1 2.8 ms 1.4 ms 4.2 ms 1.8 MB

Tested on PHP 8.5 with the JIT enabled. See the benchmark branch to run the same test.

Installation

Usage

Output:

Precompilation

Templates and partials can be precompiled to native PHP for later execution, avoiding the overhead of parsing and compilation on each request.

Build step - compile all templates in a directory and cache the generated PHP:

Runtime - load only needed templates, with precompiled partials resolved on demand:

Each {{> partial}} call triggers the resolver on first use, and the result is cached for the rest of that render. Only the partials that the page actually references are ever loaded.

[!IMPORTANT]
Precompiled templates must be regenerated whenever PHP Handlebars is updated, as the generated PHP code depends on the current version of the runtime. The build step above should be part of a deployment process so that precompiled output does not need to be committed to source control.

Compile Options

You can alter the template compilation by passing an Options instance as the second argument to compile or precompile. For example, the strict option may be set to true to generate a template which will throw an exception for missing data:

Available Options

[!NOTE]
Recursive lookup has a runtime cost, so it is recommended that performance-sensitive operations should avoid compat mode and instead opt for explicit path references.

Runtime Options

Handlebars::compile returns a closure which can be invoked as $template($context, $options). The $options parameter takes an array of runtime options, accepting the following keys:

Custom Helpers

Helper functions will be passed any arguments provided to the helper in the template. If needed, a final $options parameter can be included which will be passed a HelperOptions instance.

For example, a custom #equals helper with JS equality semantics could be implemented as follows:

HelperOptions Properties

HelperOptions Methods

[!NOTE]
isset($options->fn) and isset($options->inverse) return true if the helper was called as a block, and false for inline helper calls.

Hooks

If a custom helper named helperMissing is defined, it will be called when a mustache or a block-statement is not a registered helper AND is not a property of the current evaluation context.

If a custom helper named blockHelperMissing is defined, it will be called when a block-expression calls a helper that is not registered, even when the name matches a property in the current evaluation context.

For example:

Output:

Missing foo(2,value)
'person' not found. Printing block: John Doe

String Escaping

If a custom helper is executed in a {{ }} expression, the return value will be HTML escaped. When a helper is executed in a {{{ }}} expression, the original return value will be output directly.

Helpers may return a DevTheorem\Handlebars\SafeString instance to prevent escaping the return value. Because SafeString bypasses the automatic HTML escaping that {{ }} applies, any user-supplied content embedded in it must first be escaped with Handlebars::escapeExpression() to prevent XSS vulnerabilities.

Data Frames

Block helpers that inject @-prefixed variables should create a child data frame using Handlebars::createFrame($options->data), add their variables to it, and pass it to fn() or inverse() via the data key (e.g. $options->fn($context, ['data' => $frame])). This mirrors Handlebars.createFrame() in Handlebars.js, isolating the helper's variables while still inheriting parent data such as @root.

Missing Features

All syntax and language features from Handlebars.js 4.7.9 should work the same in PHP Handlebars, with the following exceptions:

Mustache Compatibility

Handlebars is largely compatible with Mustache syntax, with a few notable differences:


All versions of php-handlebars with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
devtheorem/php-handlebars-parser Version ^2.1
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 devtheorem/php-handlebars contains the following files

Loading the files please wait ...