⚡️ Simple and fastly template engine for PHP

Overview

EasyTpl

License Php Version GitHub tag (latest SemVer) Actions Status zh-CN readme

⚡️ Simple and fastly template engine for PHP

Features

  • It's simple, lightweight and fastly.
    • No learning costs, syntax like PHP template
    • It is simply processed and converted into native PHP syntax
    • Compatible with PHP native syntax
  • support simple echo print syntax. eg: {{= $var }} {{ $var }} {{ echo $var }}
  • support all control syntax. such as if,elseif,else;foreach;for;switch
  • support chained access array value. eg: {{ $arr.0 }} {{ $map.name }} {{ $map.user.name }}
  • More secure, the output will be processed automatically through htmlspecialchars by default
    • You can set to disable output filtering or manually use the raw filter
  • support php builtin function as filters. eg: {{ $var | ucfirst }}
  • support add custom filters.
    • default builtin filters: upper lower nl
  • support add custom directive.
  • support comments in templates. eg: {{# comments ... #}}

Install

  • Required PHP 8.0+

composer

composer require phppkg/easytpl

Quick start

use PhpPkg\EasyTpl\EasyTemplate;

$tplCode = <<<'CODE'
My name is {{ $name | strtoupper }},
My develop tags:

{{ foreach($tags as $tag) }}
- {{ $tag }}

{{ endforeach }}
CODE;

$t = new EasyTemplate();

$str = $t->renderString($tplCode, [
    'name' => 'inhere',
    'tags' => ['php', 'go', 'java'],
]);

echo $str;

Output:

My name is INHERE,
My develop tags:

- php
- go
- java

More usage

config template

use PhpPkg\EasyTpl\EasyTemplate;

$t = EasyTemplate::new([
    'tplDir'   => 'path/to/templates',
    'allowExt' => ['.php', '.tpl'],
]);

// do something ...

more settings:

/** @var PhpPkg\EasyTpl\EasyTemplate $t */
$t->disableEchoFilter();
$t->addFilter($name, $filterFn);
$t->addFilters([]);
$t->addDirective($name, $handler);

echo variable

The following statements are the same, can be used to print out variable values

{{ $name }}
{{= $name }}
{{ echo $name }}

By default, the output result will be automatically processed through htmlspecialchars, unless disabled or manually used raw filter

  • Set to disable output filtering $t->disableEchoFilter()
  • Disable output filtering in the template {{ $name | raw }}

chained access array

Can use . to quick access array value.

$arr = [
    'val0',
    'subKey' => 'val1',
];

Use in template:

First value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1

add comments

{{# comments ... #}}{{ $name }} // inhere

multi lines:

{{#
 this
 comments
 block
#}}{{ $name }} // inhere

Use Filters

Default built-in filters:

  • upper - equals strtoupper
  • lower - equals strtolower
  • nl - append newline \n

Using the filters

You can use the filters in any of your templates.

Regular usage:

{{ 'inhere' | ucfirst }} // Inhere 
{{ 'inhere' | upper }} // INHERE

Chained usage:

{{ 'inhere' | ucfirst | substr:0,2 }} // In
{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31

Passing non-static values:

{{ $name | ucfirst | substr:0,1 }}
{{ $user['name'] | ucfirst | substr:0,1 }}
{{ $userObj->name | ucfirst | substr:0,1 }}
{{ $userObj->getName() | ucfirst | substr:0,1 }}

Passing variables as filter parameters:

{{
    $suffix = '¥';
}}

{{ '12.75' | add_suffix:$suffix }} // 12.75¥

Custom filters

use PhpPkg\EasyTpl\EasyTemplate;

$tpl = EasyTemplate::new();
// use php built function
$tpl->addFilter('upper', 'strtoupper');

// add more
$tpl->addFilters([
    'last3chars' => function (string $str): string {
        return substr($str, -3);
    },
]);

Use in template:

{{
  $name = 'inhere';
}}

{{ $name | upper }} // INHERE
{{ $name | last3chars }} // ere
{{ $name | last3chars | upper }} // ERE

Custom directives

You can use the directives implement some special logic.

$tpl = EasyTemplate::new();
$tpl->addDirective(
    'include',
    function (string $body, string $name) {
        /** will call {@see EasyTemplate::include()} */
        return '$this->' . $name . $body;
    }
);

Use in template:

{{ include('part/header.tpl', ['title' => 'My world']) }}

Dep packages

License

MIT

You might also like...
A PHP project template with PHP 8.1, Laminas Framework and Doctrine

A PHP project template with PHP 8.1, Laminas Framework and Doctrine

SwitchBlade: Custom Directives for the Laravel Blade templating engine

SwitchBlade: Custom Directives for the Laravel Blade templating engine

Experimental ActiveRecord layer on top of Doctrine2 using the Twig templating engine

This is an experiment for building ActiveRecord functionality on top of Doctrine2 using the Twig templating engine. Whether it is called Propel2 or not is irrelevant.

Twig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHP

Twig, the flexible, fast, and secure template language for PHP Twig is a template language for PHP, released under the new BSD license (code and docum

A complete and fully-functional implementation of the Jade template language for PHP

Tale Jade for PHP Finally a fully-functional, complete and clean port of the Jade language to PHP — Abraham Lincoln The Tale Jade Template Engine brin

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

Foil brings all the flexibility and power of modern template engines to native PHP templates

Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simple, clean and concise templates with nothing more than PHP.

A SilverStripe Module with template methods to quickly make use of FocusPoint, LazySizes, and Object-fit

LazyFocusFit A SilverStripe module with template methods to quickly make use of FocusPoint, LazySizes and object-fit. Requirements PHP FocusPoint JS/C

Comments
Releases(v1.1.2)
  • v1.1.2(Dec 29, 2022)

    Change Log

    Feature

    • b4bfc01 feat: auto append var prefix: $ like {{var_name}}

    Fixed

    • cec4a8e fix: parse error on '-' in array subkey. eg: {{ .pkg-name }}

    Other

    • 52d3f93 ci: update the release action script
    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Dec 9, 2021)

    Change Log

    • up: update some method sign for compiler class https://github.com/phppkg/easytpl/commit/1002991b0299b349e92ddfab361c201156278a6a
    • up: update dep version https://github.com/phppkg/easytpl/commit/91b74575b09be5fa02e8fd195cd41306e9ebc187
    Source code(tar.gz)
    Source code(zip)
  • v1.0.4(Nov 29, 2021)

    Change Log

    • fix: clear comments fail on has sepcial chars https://github.com/phppkg/easytpl/commit/a5a0cb45d7bda2d06a549e4b6621c1855a61a03d
    Source code(tar.gz)
    Source code(zip)
  • v1.0.3(Nov 16, 2021)

    Change Log

    • fix: compile filter string params not add quotes https://github.com/phppkg/easytpl/commit/5615bee7e5585d714cba5dfacb73a4b60bc3535b
    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Nov 15, 2021)

    Change Log

    Feature

    • feat: support add comments in template https://github.com/phppkg/easytpl/commit/89a4a8fe9e7c0c4f5df661eaed4bd771effad0a0

    Update

    • add an jekyll doc theme https://github.com/phppkg/easytpl/commit/9009e6459e2c39bafc0f6d60a96ab3faa58bf89a
    • up: update some comments, add more tests https://github.com/phppkg/easytpl/commit/171822776b459c1bcd5cec5268924b192999f0e2
    • up: update readme add docsfiy config https://github.com/phppkg/easytpl/commit/67c3eb798f2bd8257a59c590aea744afbde23bfa
    • update readme https://github.com/phppkg/easytpl/commit/0713b3f8f2d3df5bff9c2e0bd55ce770d5994c9f
    • update: add more tests and update readme https://github.com/phppkg/easytpl/commit/3aac8e1f4e4dc6c1fd69d38150d5782f3433c4e5

    Fixed

    • doc: fix readme format error https://github.com/phppkg/easytpl/commit/ba9b48b2f649a2dbb04bf6e26fcea269e4e524bf

    Other

    • prof: always auto find tpl file on render file https://github.com/phppkg/easytpl/commit/08d2be6c54c13b10647cd980fcaafc21dceaf0c0
    • style: update readme https://github.com/phppkg/easytpl/commit/b0a578964806d3e4b842bb23c4d0333fc0201b35
    • doc: update readme exampels https://github.com/phppkg/easytpl/commit/a80b408d3165f8b512188509646351966f748df6
    • doc: add more doc on readme https://github.com/phppkg/easytpl/commit/dc9dcf64d838a6c065e1bc2de2ca7f760b7af5f8
    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Nov 11, 2021)

    Change Log

    Fixed

    • fix: not register built in filters on init text template https://github.com/phppkg/easytpl/commit/29662998f1529c1a3419eb79ab0832b0e76d157f
    • fix: fix readme example code error https://github.com/phppkg/easytpl/commit/2a135135c74126406f34d9f94315fa9b4735a928

    Other

    • prof: only compile on cached tmp file not exists https://github.com/phppkg/easytpl/commit/e631751d320b141b7f19a56365e03f9cb7a6055e
    • style: update readme https://github.com/phppkg/easytpl/commit/9b5bc882477a185eeffbaba30c1b65e3ec2599cb
    • style: update readme example https://github.com/phppkg/easytpl/commit/846abf93ab478a570360cd5bbdb1d2d3337b69b9
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Nov 11, 2021)

    Change Log

    Update

    • up: rename package name to easytpl https://github.com/phppkg/easytpl/commit/7929e811764fc4a63544408903fcdfbfb13d0920
    • update readme and deps https://github.com/phppkg/easytpl/commit/a73c224020770c6ba983dc08e2f540ed54ffd0fd
    • update: migrate template logic and tests from inhere/kite https://github.com/phppkg/easytpl/commit/fc064820e21ddc8b5369e17abc0cf4d581b2c892
    • update: add more unit tests, update readme https://github.com/phppkg/easytpl/commit/1df2b2a99781d71bae678ae3bd041c36ad6869e2

    Other

    • Initial commit https://github.com/phppkg/easytpl/commit/dd4bc85a6d5c8344766710d0c741629aed843335
    • chore: init project https://github.com/phppkg/easytpl/commit/e788709721f4344b6141025d235281dbb813e46f
    • style: update readme https://github.com/phppkg/easytpl/commit/85675474d5bae30d939947a878ba23932525ddc3
    • chore: add zh-CN readme, update readme https://github.com/phppkg/easytpl/commit/95420b75848484de2ab55b84abd994a93acb2739
    • chore: update some comments, update readme https://github.com/phppkg/easytpl/commit/77199660a719b70b20e3aa6f9b5afa7681cc9adb
    Source code(tar.gz)
    Source code(zip)
Owner
PHPPkg
Some useful package for PHP
PHPPkg
PHPlater, a simple template engine.

PHPlater A simple PHP template engine that lets PHP do all the logic and then append it to the HTML in the template file. It is set to solve the probl

John Larsen 2 Jun 3, 2022
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.

Latte: amazing template engine for PHP Introduction Latte is a template engine for PHP which eases your work and ensures the output is protected again

Nette Foundation 898 Dec 25, 2022
PHP template engine for native PHP templates

FOIL PHP template engine, for PHP templates. Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simpl

Foil PHP 167 Dec 3, 2022
Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.

Smarty 3 template engine smarty.net Documentation For documentation see www.smarty.net/docs/en/ Requirements Smarty can be run with PHP 5.2 to PHP 7.4

Smarty PHP Template Engine 2.1k Jan 1, 2023
View template engine of PHP extracted from Laravel

Blade 【简体中文】 This is a view templating engine which is extracted from Laravel. It's independent without relying on Laravel's Container or any others.

刘小乐 143 Dec 13, 2022
Twig Template Engine to Phalcon PHP

Twig Template Engine to Phalcon PHP

Vinicius 4 Oct 7, 2022
Liquid template engine for PHP

Liquid is a PHP port of the Liquid template engine for Ruby, which was written by Tobias Lutke. Although there are many other templating engines for PHP, including Smarty (from which Liquid was partially inspired)

Harald Hanek 230 Aug 18, 2022
Pug (Jade) template engine for Symfony

Pug-Symfony Pug template engine for Symfony This is the documentation for the ongoing version 3.0. Click here to load the documentation for 2.8 Instal

Pug PHP 41 Dec 16, 2022
A template abstraction prototype for PHP template engines

Schranz Templating A template abstraction prototype for PHP template engines. This project should help to find a way for a general Template Render Int

Schranz Templating 16 Dec 7, 2022
Standalone Skeltch templating engine for PHP

SkeltchGo is a standalone version of Glowie Skeltch templating engine for PHP, intented to use from outside the framework.

glowie 1 Nov 5, 2021