Back to templates

Template details

EasyTpl

Simple and fast template engine for PHP that compiles templates to native PHP syntax.

About this template

EasyTpl is a lightweight PHP template engine that compiles template files into native PHP syntax for fast rendering. It provides a simple, PHP-like syntax with {{ ... }} delimiters, automatic HTML escaping, and a small footprint for embedding in any PHP application.

What is EasyTpl?

EasyTpl is an open-source template engine developed and maintained by the phppkg organization on GitHub. It accepts template strings or template files written in a mix of HTML, plain text, and {{ ... }} directives, combines them with a PHP data array, and outputs rendered text. Because templates are converted into native PHP code, the engine requires PHP to run and is installed with Composer via phppkg/easytpl. The package ships with two classes: EasyTemplate for HTML view templates with output filtering enabled, and TextTemplate for plain-text output with filtering turned off.

Key Features

  • PHP-native syntax — Templates use standard PHP control structures such as if, elseif, else, foreach, for, and switch, so there is no new template language to learn.
  • Multiple echo syntaxes — Variables can be printed as {{ name }}, {{ $name }}, {{= $name }}, or {{ echo $name }}; the $ prefix is optional and the compiler appends it automatically.
  • Chained array access — Dot notation reads nested array values directly, for example {{ $map.user.name }} or {{ $arr.0 }}, without writing PHP array access code.
  • Automatic HTML escaping — All output is passed through htmlspecialchars by default for safety; escaping can be disabled globally with disableEchoFilter() or per-variable with the raw filter.
  • Built-in and custom filters — PHP functions double as filters, so {{ $var | ucfirst }} works immediately; built-in filters include upper, lower, and nl, and new ones can be registered with addFilter().
  • Custom directives — Developers can extend the engine by adding directives through addDirective(). The built-in directives are layout, include, and contents.
  • Template inheritance — The ExtendTemplate class introduces extends, block, and endblock directives, letting child templates override specific blocks of a parent layout.
  • Template comments — Content wrapped as {{# ... #}} is ignored during rendering, including multi-line comment blocks.

Who is it for?

  • PHP developers who want a lightweight alternative to full-featured template engines for rendering view templates or email HTML without the overhead of a framework.
  • Library authors who need a small, dependency-light rendering component inside their PHP packages, with the ability to add custom directives and filters.
  • Code-generation tooling that produces text files such as source code or configuration; the TextTemplate class disables HTML escaping so generated text is not altered.
  • Teams maintaining legacy PHP applications who want to introduce a templating layer with native PHP syntax and a low learning curve.

What can you do with EasyTpl?

  • Render HTML views in a PHP app — pass a template file and a data array to renderString() or display() to receive escaped, secure HTML output.
  • Build reusable layouts — use ExtendTemplate with extends and block directives to define a master layout and override content sections in child templates.
  • Reuse partials — include headers, footers, or other snippets with {{ include('part/header.tpl', ['title' => 'My world']) }}.
  • Generate plain text — switch to TextTemplate to render unescaped text for code generation, config files, or reports.

How does EasyTpl work?

When you call renderString() or display(), EasyTpl parses the template's {{ ... }} expressions and control blocks, compiles them into native PHP code, then executes that code with the supplied variable array. The result is returned as a string or written directly to the output buffer, making it straightforward to drop into an existing PHP page or router.

Alternatives

EasyTpl's README lists several related PHP template engines as alternatives: Twig, Latte, Blade, Fenom, and Pug. Each has its own syntax and feature set; EasyTpl positions itself as simpler and faster, compiling directly to PHP.

FAQ

Is EasyTpl open source?

Yes, EasyTpl is open source and distributed under the MIT License. Its source code is hosted on GitHub under the phppkg organization.

How do I install EasyTpl?

Install EasyTpl with Composer by running composer require phppkg/easytpl. The package's PHP version requirement and latest release are shown on the repository's badge.

Does EasyTpl automatically escape output?

Yes, by default EasyTemplate runs every echoed variable through htmlspecialchars. You can disable this globally with $t->disableEchoFilter() or per-variable with the raw filter, as in {{ $name | raw }}.

Can I create custom filters?

Yes. Use $tpl->addFilter('name', callable) to register one filter or $tpl->addFilters([...]) to register several at once. Custom filters are used the same way as built-in filters, for example {{ $name | last3chars }}.

What is the difference between EasyTemplate and TextTemplate?

EasyTemplate keeps output filtering on and is meant for rendering HTML view templates. TextTemplate disables output filtering and is designed for text processing, code generation, and other non-HTML output where escaping would be unwanted.