⚡️ 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

Releases(v1.1.0)
  • 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
☕ 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 754 Jan 16, 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 163 Nov 20, 2021
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 1.9k Jan 11, 2022
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.

刘小乐 145 Aug 13, 2021
Twig Template Engine to Phalcon PHP

Twig Template Engine to Phalcon PHP

Vinicius 3 Jun 17, 2021
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
SwitchBlade: Custom Directives for the Laravel Blade templating engine

SwitchBlade: Custom Directives for the Laravel Blade templating engine

Awkward Ideas 9 Sep 2, 2021
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

Twig 7.4k Jan 16, 2022
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

Talesoft 93 May 4, 2021
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

Ilian 4 Dec 15, 2021
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

Ilian 4 Dec 15, 2021
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

Evans Hunt 8 Oct 12, 2021
Native PHP template system

Plates Plates is a native PHP template system that's fast, easy to use and easy to extend. It's inspired by the excellent Twig template engine and str

The League of Extraordinary Packages 1.3k Jan 7, 2022
A ready-to-use Model View Controller template in PHP

PHP-MVC-Template A ready-to-use Model View Controller template in PHP Use this repo as a template! (Or clone it) Start to configure your MVC file Afte

Loule | Louis 7 Dec 28, 2021
Provides a GitHub repository template for a PHP package, using GitHub actions.

php-package-template Installation ?? This is a great place for showing how to install the package, see below: Run $ composer require ergebnis/php-pack

null 262 Jan 16, 2022
A lightweight template parser used by PyroCMS.

Lex Lex is a lightweight template parser. Lex is released under the MIT License and is Copyrighted 2011 - 2014 PyroCMS Team. Change Log 2.3.2 Convert

PyroCMS 102 Nov 19, 2021
Laravel package template

REPLACE Simple and flexible package template. Usage Replace all occurances of REPLACE (case sensitive) with the name of the package namespace. E.g. th

ARCHTECH 48 Dec 4, 2021
The Templating component provides all the tools needed to build any kind of template system.

Templating Component The Templating component provides all the tools needed to build any kind of template system. It provides an infrastructure to loa

Symfony 984 Jan 10, 2022
Document templates Laravel package is intended for creating/managing user editable document template

Document Templates Introduction Document templates Laravel package is intended for creating/managing user editable document templates, with ability to

42coders 111 Dec 23, 2021