{"id":22425359,"url":"https://github.com/phppkg/easytpl","last_synced_at":"2025-08-01T08:32:58.314Z","repository":{"id":43414643,"uuid":"425018062","full_name":"phppkg/easytpl","owner":"phppkg","description":"⚡️ Simple and fastly template engine for PHP.","archived":false,"fork":false,"pushed_at":"2024-03-23T04:07:46.000Z","size":105,"stargazers_count":23,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-24T13:36:40.412Z","etag":null,"topics":["html-template","php","render-template","renderer","rendering-engine","template","template-engine","text-template"],"latest_commit_sha":null,"homepage":"https://phppkg.github.io/easytpl/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phppkg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-11-05T16:40:13.000Z","updated_at":"2023-12-13T07:59:37.000Z","dependencies_parsed_at":"2023-11-10T15:24:57.588Z","dependency_job_id":"d8356bd3-bb5a-4cb5-8b1c-147e41f3825e","html_url":"https://github.com/phppkg/easytpl","commit_stats":{"total_commits":56,"total_committers":3,"mean_commits":"18.666666666666668","dds":0.0535714285714286,"last_synced_commit":"4a30091c638667bd94d2c02afaf3525eef0d7f60"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":"inherelab/php-pkg-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phppkg%2Feasytpl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phppkg%2Feasytpl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phppkg%2Feasytpl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phppkg%2Feasytpl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phppkg","download_url":"https://codeload.github.com/phppkg/easytpl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228356390,"owners_count":17907191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html-template","php","render-template","renderer","rendering-engine","template","template-engine","text-template"],"created_at":"2024-12-05T19:13:54.078Z","updated_at":"2025-08-01T08:32:58.300Z","avatar_url":"https://github.com/phppkg.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyTpl\n\n[![License](https://img.shields.io/github/license/phppkg/easytpl.svg?style=flat-square)](LICENSE)\n[![Php Version](https://img.shields.io/packagist/php-v/phppkg/easytpl?maxAge=2592000)](https://packagist.org/packages/phppkg/easytpl)\n[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/phppkg/easytpl)](https://github.com/phppkg/easytpl)\n[![Actions Status](https://github.com/phppkg/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/phppkg/easytpl/actions)\n[![zh-CN readme](https://img.shields.io/badge/中文-Readme-brightgreen.svg?style=for-the-badge\u0026maxAge=2592000)](README.zh-CN.md)\n\n⚡️ Simple and fastly template engine for PHP.\n\n## Features\n\n- It's simple, lightweight and fastly.\n  - No learning costs, syntax like PHP template\n  - It is simply processed and converted into native PHP syntax\n  - Compatible with PHP native syntax\n- support simple echo print syntax. eg: `{{ var }}` `{{= $var }}` `{{ $var }}` `{{ echo $var }}`\n  - allow ignore prefix `$`, will auto append on compile.\n- support chained access array value. eg: `{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}`\n- support all control syntax. such as `if,elseif,else;foreach;for;switch`\n- support php builtin function as filters. eg: `{{ $var | ucfirst }}`  `{{ date('Y-m-d') }}`\n- More secure, the output will be processed automatically through `htmlspecialchars` by default\n  - You can set to disable output filtering or manually use the `raw` filter\n- support add custom filters.\n  - default builtin filters: `upper` `lower` `nl`\n- support add custom directive.\n  - `EasyTemplate` built in support `layout` `include` `contents`\n  - `ExtendTemplate` built in support `extends` `block` `endblock`\n- support comments in templates. eg: `{{# comments ... #}}`\n\n## Install\n\n**composer**\n\n```bash\ncomposer require phppkg/easytpl\n```\n\n## Quick start\n\n```php\nuse PhpPkg\\EasyTpl\\EasyTemplate;\n\n$tplCode = \u003c\u003c\u003c'CODE'\nMy name is {{ $name | strtoupper }},\nMy develop tags:\n\n{{ foreach($tags as $tag) }}\n- {{ $tag }}\n\n{{ endforeach }}\nCODE;\n\n$t = new EasyTemplate();\n\n$str = $t-\u003erenderString($tplCode, [\n    'name' =\u003e 'inhere',\n    'tags' =\u003e ['php', 'go', 'java'],\n]);\n\necho $str;\n```\n\n**Output**:\n\n```text\nMy name is INHERE,\nMy develop tags:\n\n- php\n- go\n- java\n```\n\n## More usage\n\nThe syntax is the same as the PHP native template, and the special syntax added is just to make it more convenient to use.\n\n- `EasyTemplate` enables output filtering by default, which can be used to render view templates.\n- `TextTemplate` turns off output filtering and is mainly used for text processing, code generation, etc.\n\n### Config template\n\n```php\nuse PhpPkg\\EasyTpl\\EasyTemplate;\n\n$t = EasyTemplate::new([\n    'tplDir'   =\u003e 'path/to/templates',\n    'allowExt' =\u003e ['.php', '.tpl'],\n]);\n\n// do something ...\n```\n\nmore settings：\n\n```php\n/** @var PhpPkg\\EasyTpl\\EasyTemplate $t */\n$t-\u003edisableEchoFilter();\n$t-\u003eaddFilter($name, $filterFn);\n$t-\u003eaddFilters([]);\n$t-\u003eaddDirective($name, $handler);\n```\n\n### Echo variable\n\nThe following statements are the same, can be used to print out variable values\n\n```php\n{{ name }}\n{{ $name }}\n{{= $name }}\n{{ echo $name }}\n```\n\nMore:\n\n```php\n{{ $name ?: 'inhere' }}\n{{ $age \u003e 20 ? '20+' : '\u003c= 20' }}\n```\n\n\u003e By default, the output result will be automatically processed through `htmlspecialchars`,\n\u003e unless disabled or manually used `raw` filter\n\n- Set to disable output filtering `$t-\u003edisableEchoFilter()`\n- Disable output filtering in the template `{{ $name | raw }}`\n\n### Chained access array\n\nCan use `.` to quick access array value.\n\n```php\n$arr = [\n    'val0',\n    'subKey' =\u003e 'val1',\n];\n```\n\nUse in template:\n\n```php\nFirst value is: {{ $arr.0 }} // val0\n'subKey' value is: {{ $arr.subKey }} // val1\n```\n\n### If blocks\n\nonly `if`:\n\n```php\n{{ if ($name !== '') }}\nhi, my name is {{ $name }}\n{{ endif }}\n```\n\n`if else`:\n\n```php\nhi, my name is {{ $name }}\nage is {{ $age }}, and\n{{ if ($age \u003e= 20) }}\n age \u003e= 20.\n{{ else }}\n age \u003c 20.\n{{ endif }}\n```\n\n`if...elseif...else`:\n\n```php\nhi, my name is {{ $name }}\nage is {{ $age }}, and\n{{ if ($age \u003e= 50) }}\n age \u003e= 50.\n{{ elseif ($age \u003e= 20) }}\n age \u003e= 20.\n{{ else }}\n age \u003c 20.\n{{ endif }}\n```\n\n### For/Foreach blocks\n\n`foreach`:\n\n```php\ntags:\n\n{{ foreach($tags as $tag) }}\n- {{ $tag }}\n\n{{ endforeach }}\n```\n\nwith keys:\n\n```php\ntags:\n\n{{ foreach($tags as $index =\u003e $tag) }}\n{{ $index }}. {{ $tag }}\n\n{{ endforeach }}\n```\n\n### Add comments\n\nThe contents wrapped with `{{` and `}}` will be ignored as comments.\n\n```php\n{{# comments ... #}}{{ $name }} // inhere\n```\n\nmulti lines:\n\n```php\n{{#\n this\n comments\n block\n#}}{{ $name }} // inhere\n```\n\n## Use Filters\n\nDefault built-in filters: \n\n- `upper` - equals `strtoupper`\n- `lower` - equals `strtolower`\n- `nl` - append newline `\\n`\n\n### Using the filters\n\nYou can use the filters in any of your templates.\n\n**Regular usage**:\n\n```php\n{{ 'inhere' | ucfirst }} // Inhere \n{{ 'inhere' | upper }} // INHERE\n```\n\n**Chained usage**:\n\n```php\n{{ 'inhere' | ucfirst | substr:0,2 }} // In\n{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31\n```\n\n**Passing non-static values**:\n\n```php\n{{ $name | ucfirst | substr:0,1 }}\n{{ $user['name'] | ucfirst | substr:0,1 }}\n{{ $userObj-\u003ename | ucfirst | substr:0,1 }}\n{{ $userObj-\u003egetName() | ucfirst | substr:0,1 }}\n```\n\n**Passing variables as filter parameters**:\n\n```php\n{{\n    $suffix = '￥';\n}}\n\n{{ '12.75' | add_suffix:$suffix }} // 12.75￥\n```\n\n### Custom filters\n\n```php\nuse PhpPkg\\EasyTpl\\EasyTemplate;\n\n$tpl = EasyTemplate::new();\n// use php built function\n$tpl-\u003eaddFilter('upper', 'strtoupper');\n\n// add more\n$tpl-\u003eaddFilters([\n    'last3chars' =\u003e function (string $str): string {\n        return substr($str, -3);\n    },\n]);\n```\n\nUse in template:\n\n```php\n{{\n  $name = 'inhere';\n}}\n\n{{ $name | upper }} // INHERE\n{{ $name | last3chars }} // ere\n{{ $name | last3chars | upper }} // ERE\n```\n\n## Custom directives\n\nYou can use the directives implement some special logic.\n\n\u003e`EasyTemplate` built in support: `layout` `include` `contents`\n\n```php\n$tpl = EasyTemplate::new();\n$tpl-\u003eaddDirective(\n    'include',\n    function (string $body, string $name) {\n        /** will call {@see EasyTemplate::include()} */\n        return '$this-\u003einclude' . $body;\n    }\n);\n```\n\n------\n\n### Use layout\n\n- page template `home01.tpl`\n\n```php\n{{ layout('layouts/layout01.tpl') }}\n\non home: block body;\n```\n\n### Use include\n\n**Use in template**\n\n```php\n{{ include('part/header.tpl', ['title' =\u003e 'My world']) }}\n```\n\n------\n\n## Extends template\n\nNew directives:\n\n- `extends` extends a layout template file.\n  - syntax: `{{ extends('layouts/main.tpl') }}`\n- `block` define a new template block start.\n  - syntax: `{{ block 'header' }}`\n- `endblock` mark a block end.\n  - syntax: `{{ endblock }}`\n\n```php\nuse PhpPkg\\EasyTpl\\ExtendTemplate;\n\n$et = new ExtendTemplate();\n$et-\u003edisplay('home/index.tpl');\n```\n\n### Examples for extend\n\n- on layout file: `layouts/main.tpl`\n\n```php\n{{ block 'header' }}\nheader contents in layout main.\n{{ endblock }}\n\n{{ block 'body' }}\nbody contents in layout main.\n{{ endblock }}\n\n{{ block 'footer' }}\nfooter contents in layout main.\n{{ endblock }}\n```\n\n- on page file: `home/index.tpl`\n\n```php\n{{ extends('layouts/main.tpl') }}\n\n{{ block 'body' }}\nbody contents in home index.\n{{ endblock }}\n```\n\n**Rendered results**\n\n```text\nheader contents in layout main.\nbody contents in home index.\nfooter contents in layout main.\n```\n\n------\n\n## Dep packages\n\n- [toolkit/fsutil](https://github.com/php-toolkit/fsutil)\n- [toolkit/stdlib](https://github.com/php-toolkit/stdlib)\n\n## Related\n\n- https://github.com/twigphp/Twig\n- https://github.com/nette/latte\n- https://github.com/jenssegers/blade\n- https://github.com/fenom-template/fenom\n- https://github.com/pug-php/pug\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphppkg%2Feasytpl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphppkg%2Feasytpl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphppkg%2Feasytpl/lists"}