{"id":15025767,"url":"https://github.com/wol-soft/php-micro-template","last_synced_at":"2026-03-01T23:10:31.707Z","repository":{"id":57081607,"uuid":"131174165","full_name":"wol-soft/php-micro-template","owner":"wol-soft","description":"A minimalistic templating engine for PHP","archived":false,"fork":false,"pushed_at":"2025-09-12T12:47:31.000Z","size":83,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-26T11:43:00.081Z","etag":null,"topics":["lightweight","php","php7","php8","php81","php82","php83","php84","regex","template-engine","templating"],"latest_commit_sha":null,"homepage":"","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/wol-soft.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,"publiccode":null,"codemeta":null}},"created_at":"2018-04-26T15:15:39.000Z","updated_at":"2025-09-12T12:47:34.000Z","dependencies_parsed_at":"2025-02-15T19:31:31.551Z","dependency_job_id":"00b0e2da-5493-4353-a9a4-bd6239fb8a7a","html_url":"https://github.com/wol-soft/php-micro-template","commit_stats":{"total_commits":39,"total_committers":3,"mean_commits":13.0,"dds":0.5128205128205128,"last_synced_commit":"fe6f91d1db306dd2bac08b94d07f0f4d35bf43f5"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/wol-soft/php-micro-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wol-soft%2Fphp-micro-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wol-soft%2Fphp-micro-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wol-soft%2Fphp-micro-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wol-soft%2Fphp-micro-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wol-soft","download_url":"https://codeload.github.com/wol-soft/php-micro-template/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wol-soft%2Fphp-micro-template/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29987656,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T22:42:38.399Z","status":"ssl_error","status_checked_at":"2026-03-01T22:41:51.863Z","response_time":124,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["lightweight","php","php7","php8","php81","php82","php83","php84","regex","template-engine","templating"],"created_at":"2024-09-24T20:02:58.948Z","updated_at":"2026-03-01T23:10:31.700Z","avatar_url":"https://github.com/wol-soft.png","language":"PHP","readme":"[![Latest Version](https://img.shields.io/packagist/v/wol-soft/php-micro-template.svg)](https://packagist.org/packages/wol-soft/php-micro-template)\n[![Maintainability](https://qlty.sh/gh/wol-soft/projects/php-micro-template/maintainability.svg)](https://qlty.sh/gh/wol-soft/projects/php-micro-template)\n[![Build Status](https://github.com/wol-soft/php-micro-template/actions/workflows/main.yml/badge.svg)](https://github.com/wol-soft/php-micro-template/actions/workflows/main.yml)\n[![Coverage Status](https://coveralls.io/repos/github/wol-soft/php-micro-template/badge.svg?branch=master)](https://coveralls.io/github/wol-soft/php-micro-template?branch=master)\n[![MIT License](https://img.shields.io/packagist/l/wol-soft/php-micro-template.svg)](https://github.com/wol-soft/php-micro-template/blob/master/LICENSE)\n\n# php-micro-template\nA minimalistic, lightweight templating engine for PHP with zero dependencies based on regular expressions.\n\n## Features ##\n\n- Replace variables inside a template\n- Iterate over an array or iterable object\n- Conditional sections\n- Pass objects\n- call functions\n\n## Requirements ##\n\n- Requires at least PHP 7.1\n\n## Installation ##\n\nThe recommended way to install php-micro-template is through [Composer](http://getcomposer.org):\n```\n$ composer require wol-soft/php-micro-template\n```\n\n## Examples ##\n\nFirst create your template file:\n\n```html\n\u003chtml\u003e\n    \u003ch1\u003e{{ pageTitle }}\u003c/h1\u003e\n\n    \u003cul class=\"row\"\u003e\n        {% foreach products as product %}\n            {% if product.isVisible() %}\n                \u003cli class=\"product\"\u003e\n                    \u003cspan\u003e{{ productHead }}\u003c/span\u003e\n                    \u003cspan\u003e{{ product.getTitle() }}\u003c/span\u003e\n                \u003c/li\u003e\n            {% endif %}\n        {% endforeach %}\n    \u003c/ul\u003e\n\n    {% if showVersion %}\n        \u003cdiv class=\"version\"\u003e1.0.1\u003c/div\u003e\n    {% endif %}\n\u003c/html\u003e\n```\n\nAfterwards create a new instance of the Render class and render your template:\n\n```php\n\u003c?php\n\nuse PHPMicroTemplate\\Render;\n\n/* ... */\n\n$render = new Render(__DIR__ . '/Templates/');\n\n$result = $render-\u003erenderTemplate(\n    'productList.template',\n    [\n        'pageTitle' =\u003e $translator-\u003etranslate('availableProducts'),\n        'productHead' =\u003e $translator-\u003etranslate('product'),\n        'products' =\u003e $products,\n        'showVersion' =\u003e true\n    ]\n);\n\n/* ... */\n```\n\nInstead of saving your templates into files you can also prepare a string which contains the template:\n\n```php\n\u003c?php\n\nuse PHPMicroTemplate\\Render;\n\n/* ... */\n\n$myPartialTemplate = '\n    {% foreach products as product %}\n        {% if product.isVisible() %}\n            \u003cli class=\"product\"\u003e\n                \u003cspan\u003e{{ productHead }}\u003c/span\u003e\n                \u003cspan\u003e{{ product.getTitle() }}\u003c/span\u003e\n            \u003c/li\u003e\n        {% endif %}\n    {% endforeach %}\n';\n\n$render = new Render();\n\n$result = $render-\u003erenderTemplateString(\n    $myPartialTemplate,\n    [\n        'productHead' =\u003e $translator-\u003etranslate('product'),\n        'products' =\u003e $products\n    ]\n);\n\n/* ... */\n```\n\n### Replacement of variables\n\nValues which are assigned to the template and used directly will be casted to string.\nFor assigned objects you can call methods which return a value.\nAfterwards the returned value will be casted to string.\nAs constant values integer numbers, strings in single quotes and booleans (true, false) are supported. \n\n```html\n{{ simpleValue }}\n{{ myObject.getProperty() }}\n{{ 'Hello World' }}\n{{ 12345 }}\n```\n\nYour provided data may be a nested array which can be resolved in the template:\n\n```php\n$render-\u003erenderTemplateString(\n    '{{ render.productRender.renderProductName(product.details.name) }}',\n    [\n        'product' =\u003e [\n            'details' =\u003e [\n                'name' =\u003e 'MyProduct',\n            ],\n        ],\n        'render' =\u003e [\n            'productRender' =\u003e new ProductRender(),\n        ]\n    ]\n);\n```\n\nAlso, public properties of objects may be accessed from the template:\n\n```php\n$person = new stdClass();\n$person-\u003ename = 'Hans';\n\n$render-\u003erenderTemplateString(\n    '{{ person.name }}',\n    [\n        'person' =\u003e $person,\n    ]\n);\n```\n\nBy default, a used variable which is not provided will result in an `UndefinedSymbolException`. You can register a callback function via `onResolveError` to handle unresolved variable errors. The callback function must implement the signature `function (string $unresolvedVariable): string`. The provided `$unresolvedVariable` will contain the whole expression which failed to resolve (eg. `myUnresolvedVariable`, `myUnresolvedObject.render(var1, var2)`).\n\n```php\n$render-\u003eonResolveError(function (string $var): string {\n    return 'Undefined';\n});\n\n// will result in \"Person name: Undefined\"\n$result = $render-\u003erenderTemplateString('Person name: {{ name }}');\n```\n\n### Loops\n\nIf you assign an array or an iterable object you can use the *foreach* loop to iterate.\n\n```html\n{% foreach products as product %}\n    \u003cspan\u003e{{ product.getTitle() }}\u003c/span\u003e\n{% endforeach %}\n```\n\nAll variables of the parent scope are available inside the loop as well as the current item of the loop. Multiple foreach loops can be nested (compare tests). You can also provide a function which returns an array or an iterable object:\n\n```html\n{% foreach product.getIngredients() as ingredient %}\n    \u003cspan\u003e{{ ingredient.getTitle() }}\u003c/span\u003e\n{% endforeach %}\n```\n\nLoops support the usage of key value pairs:\n\n```html\n{% foreach products as bestSellerNumber, product %}\n    \u003cb\u003eBestseller Nr. {{ bestSellerNumber }}:\u003c/b\u003e{{ product.getTitle() }}\u003cbr/\u003e\n{% endforeach %}\n```\n\n### Conditional sections\n\nWith the *if* statement you can create conditional sections. As a condition you can pass either a value which will be casted to bool or call a method on an object. In this case the return value of the function will be casted to bool.\nNeither multiple values in a single condition combined by operators nor calculations or similar additional functions are provided. For advanced conditions compare the section *function calls* with a ViewHelper-Object.\n\n```html\n{% if showProducts %}\n    {% if product.isVisible() %}\n        \u003cspan\u003e{{ product.getTitle() }}\u003c/span\u003e\n    {% else %}\n        \u003cspan\u003eProduct {{ product.getTitle() }} currently not available\u003c/span\u003e\n    {% endif %}\n{% endif %}\n```\n\nMultiple if statements can be nested. To invert an if condition the keyword *not* can be used:\n\n```html\n{% if not product.isVisible() %}\n    \u003cspan\u003eProduct {{ product.getTitle() }} currently not available\u003c/span\u003e\n{% endif %}\n```\n\nTo compose multiple conditions in a single if, the keywords *and* and *or* can be used (*and* has the higher precedence). Brackets in if statements are not supported.\n\n```html\n{% if not product.isVisible() or not product.isAvailable() %}\n    \u003cspan\u003eProduct {{ product.getTitle() }} currently not available\u003c/span\u003e\n{% endif %}\n```\n\n### function calls\n\nThe methods which are called can take parameters.\nAllowed parameters are variables taken out of the current scope or another function call on an object available in the current scope as well as the supported constant values integer numbers, strings in single quotes and booleans (true, false).\nAs an example a ViewHelper-Object can be assigned to the render process and methods of the ViewHelper can be used in the template for advanced logic inside the template.\n\n```php\n\u003c?php\n\nuse PHPMicroTemplate\\Render;\n\n/* ... */\n\nclass ViewHelper\n{\n    public function count(iterable $list): int\n    {\n        return count($list);\n    }\n\n    public function sum(float ...$values): float\n    {\n        return array_sum($values);\n    }\n\n    public function weight(string $label, int $weight = 400): string\n    {\n        return sprintf('\u003cspan style=\"font-weight: %d;\"\u003e%s\u003c/span\u003e', $weight, $label);\n    }\n}\n\n/* ... */\n\n$render = new Render(__DIR__ . '/Templates/');\n\n$result = $render-\u003erenderTemplate(\n    'functionExample.template',\n    [\n        'viewHelper' =\u003e new ViewHelper(),\n        'currencyFormatter' =\u003e new CurrencyFormatter(),\n        'basePrice' =\u003e 3.00,\n        'products' =\u003e $products\n    ]\n);\n\n/* ... */\n\n```\n\n```html\n\u003chtml\u003e\n    \u003cp\u003eProducts: {{ viewHelper.count(products) }}\n    \u003cul class=\"row\"\u003e\n        {% foreach products as product %}\n            \u003cli class=\"product\"\u003e\n                \u003cspan\u003e{{ viewHelper.weightFont(product.getTitle(), 600) }}\u003c/span\u003e\n                \u003cspan\u003ePrice: {{\n                    currencyFormatter.format(\n                        viewHelper.sum(\n                            product.getPrice(),\n                            basePrice\n                        )\n                    )\n                }}\u003c/span\u003e\n            \u003c/li\u003e\n        {% endforeach %}\n    \u003c/ul\u003e\n\u003c/html\u003e\n```\n\nAdditionally, PHP global functions can be used directly in the template as well as assigned callback methods:\n\n```php\n\u003c?php\n\nuse PHPMicroTemplate\\Render;\n\n/* ... */\n\n$render = new Render(__DIR__ . '/Templates/');\n\n$result = $render-\u003erenderTemplate(\n    'functionExample.template',\n    [\n        'customCallback' =\u003e function(string $in): string {\n            return trim(strtoupper($in));\n        },\n    ]\n);\n\n/* ... */\n\n```\n\n```html\n\u003chtml\u003e\n    \u003cp\u003e{{ customCallback('products') }}\u003c/p\u003e\n    \u003cspan\u003e{{ strtolower('UNDER CONSTRUCTION') }}\u003c/span\u003e\n\u003c/html\u003e\n```\n\n### Whitespace tolerance\n\nThe templating syntax is whitespace tolerant so a template like the one below would be perfectly fine:\n\n```html\n{%if\n    product.getCategories()\n%}\n    \u003cp\u003eCategories:\u003c/p\u003e\n    \u003cul\u003e\n    {%foreach\n         product.getCategories()\n            as\n         category\n    %}\n        \u003cli\u003e{{product.getTitle()} [{{   category   }}]\u003c/li\u003e\n    {%endforeach%}\n    \u003c/ul\u003e\n{%endif%}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwol-soft%2Fphp-micro-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwol-soft%2Fphp-micro-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwol-soft%2Fphp-micro-template/lists"}