{"id":18750664,"url":"https://github.com/webiny/templateengine","last_synced_at":"2025-11-26T20:30:17.955Z","repository":{"id":20228873,"uuid":"23500742","full_name":"webiny/TemplateEngine","owner":"webiny","description":"[READ-ONLY] PHP  `TemplateEngine` component provides a layer for rendering view templates. Currently supports Smarty, but you can easily integrate Twig or some other. (master at Webiny/Framework) ","archived":false,"fork":false,"pushed_at":"2017-11-26T21:25:36.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-12-28T22:53:27.913Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"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/webiny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-30T21:07:24.000Z","updated_at":"2018-06-05T14:10:40.000Z","dependencies_parsed_at":"2022-08-22T15:40:28.073Z","dependency_job_id":null,"html_url":"https://github.com/webiny/TemplateEngine","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FTemplateEngine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FTemplateEngine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FTemplateEngine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FTemplateEngine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/TemplateEngine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239635844,"owners_count":19672241,"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":[],"created_at":"2024-11-07T17:12:43.955Z","updated_at":"2025-11-26T20:30:17.922Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Template Engine Component\n=========================\n\n`TemplateEngine` component provides a layer for rendering view templates.\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer.\n\n```bash\ncomposer require webiny/template-engine\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/template-engine).\n\n### Installation error\n\nIf you get an error like this:\n\n```\n[RuntimeException]\nPackage could not be downloaded, sh: 1: svn: not found\n```\n\nThen probably you don't have Subversion installed which is preventing the installation of Smarty library.\nThis can be easily solved by installing the Subversion, prior to the composer install.\n\n    $ sudo apt-get install subversion\n\n\n## Usage\n\nThe definition of the view template depends\non the selected driver. By default the template engine comes with a driver for `Smarty`, but you can easily add\nsupport for `Twig` or some other template engines.\n\nThe provided functionality of every driver is defined by the `TemplateEngineInterface` which defines the following methods:\n- **fetch** - fetch the template from the given location, parse it and return the output\n- **render** - fetch the template from the given location, parse it and output the result to the browser\n- **assign** - assign a variable and its value into the template engine\n- **setTemplateDir** - directory where the template files are stored\n- **registerPlugin** - register a plugin for the template engine\n\nTo create a new driver just create a new class that implements the `\\Webiny\\Component\\TemplateEngine\\Bridge\\TemplateEngineInterface`\nand adapt your config.\n\nThe default configuration looks like this:\n```yaml\n    TemplateEngine:\n        Engines:\n            Smarty:\n                ForceCompile: false\n                CacheDir: '/var/tmp/smarty/cache'\n                CompileDir: '/var/tmp/smarty/compile'\n                TemplateDir: '/var/www/theme/templates'\n                AutoEscapeOutput: false\n                MuteExpectedErrors: true\n```\n\n## Usage\n\nThe preferred usage is over the `TemplateEngineTrait`.\nHere is an example:\n\n```php\nclass MyClass\n{\n\tuse \\Webiny\\Component\\TemplateEngine\\TemplateEngineTrait;\n\n\tfunction __construct() {\n\t    // assing name and id to the template and render it\n\t\t$this-\u003etemplateEngine('Smarty')-\u003erender('template.tpl', ['name'=\u003e'John', 'id'=\u003e15]);\n\t}\n}\n```\n\n### Smarty\n\nIf you wish to use the Smarty template engine, with the built in driver, make sure you include\n`path/to/Smarty/libs/Smarty.class.php` somewhere inside you application, before using the component. This is due to the\nproblem that Smarty doesn't provide a suitable autoloader to be integrated with the component.\n\n## Plugins \u0026 extensions\n\nThe template engine is designed so that it can be expanded with different plugins and modifiers, depending on the assigned driver.\n\nBest practice for expanding the template engine is first to create an extension and then register it as a service\ntagged with the `$driverName.Extension`, for example `Smarty.Extension`.\n\nAn `Extension` is a package of one or multiple plugins. Plugin type depends on the template engine, for example, Smarty\nsupports these plugin types:\n- **functions** - http://www.smarty.net/docs/en/plugins.functions.tpl\n- **modifiers** - http://www.smarty.net/docs/en/plugins.modifiers.tpl\n- **blocks** - http://www.smarty.net/docs/en/plugins.block.functions.tpl\n- **compiler functions** - http://www.smarty.net/docs/en/plugins.compiler.functions.tpl\n- **pre filters** - http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl\n- **post filters** - http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl\n- **output filters** - http://www.smarty.net/docs/en/plugins.outputfilters.tpl\n- **resources** - http://www.smarty.net/docs/en/plugins.resources.tpl\n- **inserts** - http://www.smarty.net/docs/en/plugins.inserts.tpl\n\nTo create a smarty extension, create a class that extends `\\Webiny\\Component\\TemplateEngine\\Drivers\\Smarty\\AbstractSmartyExtension`\nand then overwrite the methods, based on the plugin type your wish to create.\n\nFor example, let's say we want to register a modifier called 'customUpper'. First we create our extension class like this:\n\n```php\nnamespace MyApp\\Demo;\n\nclass MySmartyExtension extends \\Webiny\\Component\\TemplateEngine\\Drivers\\Smarty\\SmartyExtension\n{\n\t/**\n\t * @overwrite\n\t * @return array\n\t */\n\tfunction getModifiers(){\n\t\treturn [\n\t\t\tnew SmartySimplePlugin('custom_upper', 'modifier', [$this, 'customUpper'])\n\t\t];\n\t}\n\n\t/**\n\t * Callback for my custom_upper modifier.\n\t *\n\t * @param $params\n\t *\n\t * @return string\n\t */\n\tfunction customUpper($params){\n\t\treturn strtoupper($params);\n\t}\n\n\t/**\n\t * Returns the name of the plugin.\n\t *\n\t * @return string\n\t */\n\tfunction getName() {\n\t\treturn 'my_extension';\n\t}\n}\n```\n\nOnce we have our extension, we must register it using the service manager:\n\n```yaml\nMyApp:\n    CustomExtension:\n        Class: \\MyApp\\Demo\\MySmartyExtension\n        Tags: [Smarty.Extension]\n```\n\nAnd that's it, we can now use the modifier in our templates:\n\n```php\n{'this is my name'|custom_upper}\n// outputs: THIS IS MY NAME\n```\n\nResources\n---------\n\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/TemplateEngine/\n    $ composer.phar install\n    $ phpunit","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Ftemplateengine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Ftemplateengine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Ftemplateengine/lists"}