{"id":15296367,"url":"https://github.com/tpawl/lite","last_synced_at":"2025-03-25T05:43:42.630Z","repository":{"id":57072256,"uuid":"98930063","full_name":"tpawl/LiTE","owner":"tpawl","description":"Lightweight Template Engine","archived":false,"fork":false,"pushed_at":"2022-01-07T20:30:56.000Z","size":989,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-25T05:43:38.827Z","etag":null,"topics":["backend","lightweight","native","php71","template-engine"],"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/tpawl.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}},"created_at":"2017-07-31T21:08:09.000Z","updated_at":"2022-01-07T20:30:59.000Z","dependencies_parsed_at":"2022-08-24T09:20:08.147Z","dependency_job_id":null,"html_url":"https://github.com/tpawl/LiTE","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpawl%2FLiTE","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpawl%2FLiTE/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpawl%2FLiTE/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpawl%2FLiTE/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tpawl","download_url":"https://codeload.github.com/tpawl/LiTE/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245407755,"owners_count":20610232,"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":["backend","lightweight","native","php71","template-engine"],"created_at":"2024-09-30T18:10:13.653Z","updated_at":"2025-03-25T05:43:42.605Z","avatar_url":"https://github.com/tpawl.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"LiTE\n====\n**Li**ghtweight **T**emplate **E**ngine\n\n[![Build Status](https://travis-ci.org/tpawl/LiTE.svg?branch=master)](https://travis-ci.org/tpawl/LiTE)\n[![Coverage Status](https://coveralls.io/repos/github/tpawl/LiTE/badge.svg?branch=master)](https://coveralls.io/github/tpawl/LiTE?branch=master)\n\nDescription\n-----------\n\n**LiTE** is an ad hoc Template Engine especially suited for Backends.\nIt is native, because it uses PHP as its Template Language and needs no compiling of templates.\nIt supports Template Variables and View Helpers.\n\nRequirements\n------------\n\n**LiTE** requires PHP 7.3+\n\nInstallation\n------------\n\nVia Composer\n\n```bash\n$ composer require tpawl/lite\n```\n\nUsage\n-----\n\n### LiTE for Developers\n\n#### Basics\n\nThe heart of **LiTE** is a object called the **template expression** (of class `TPawl\\LiTE\\Expressions\\TemplateExpression`).\n\nA template expression object is created like this:\n\n```php\n$settings = [\n    $template, // a string holding the template\n    ['the' =\u003e 'variables', 'go' =\u003e 'here'],\n    '/path/to/view_helpers',\n    'view_helpers\\namespace',\n];\n\n$templateExpression = new TPawl\\LiTE\\Expressions\\TemplateExpression($settings);\n```\nThis will create a template expression that looks up the view helpers in the `/path/to/view_helpers/` folder.\n\nNote that the only argument of the template expression is an array of settings options.\n* The first option is the template as a string.\n* The second option is an associative array of template variables, with the name of the variable as the key and its value.\n* The third option is the path to the folder in which the view helpers are stored.\n* The fourth option is the namespace in which the view helpers are defined as a string. If you do not use a namespace for your view helpers, write the empty string (`''`) here.\n\n#### Outputting the template\n\n```php\n$templateExpression-\u003edisplay();\n```\n\n#### Defining a view helper\n\nA view helper is a class that implements the interface `TPawl\\LiTE\\ViewHelperInterface`, that has a static `execute` method.\nThe name of that class must be ending with `ViewHelper` and the first letter must be upper-case.\nThe code for this class must be saved in a file with the name of the corresponding class with a trailing `.php`.\nThis file must be stored in a folder given as the third configuration option of the template expression.\n\nExample:\n\n```php\n\u003c?php\n// HelloViewHelper.php\n\nclass HelloViewHelper implements TPawl\\LiTE\\ViewHelperInterface\n{\n    public static function execute(array $arguments): void\n    {\n        print 'Hello world.';\n    }\n}\n\n```\n\n`$arguments` is an indexed array of arguments that can be given to the view helper.\nOutput in the view helper can be done with `echo`, `print`, `printf`, ...\n\n#### Sub template expression\n\nObjects of the class `TPawl\\LiTE\\Expressions\\SubTemplateExpression` are intended for use inside a view helper.\nThe constructor takes two arguments:\n* The first argument is the template as a string.\n* The second argument is an associative array of template variables, with the name of the variable as the key and its value.\n\nExample:\n\n```php\nuse TPawl\\LiTE\\Expressions\\SubTemplateExpression;\n\nclass ExampleViewHelper implements TPawl\\LiTE\\ViewHelperInterface\n{\n    public static function execute(array $arguments): void\n    {\n        $condition = $arguments[0];\n\n        if ($condition) {\n\n            $subTemplateExpression = new SubTemplateExpression(\n                $templateA, ['the' =\u003e 'variables', 'go' =\u003e 'here']);\n\n            $subTemplateExpression-\u003edisplay();\n\n        } else {\n\n            $subTemplateExpression = new SubTemplateExpression(\n                $templateB, ['the' =\u003e 'variables', 'go' =\u003e 'here']);\n\n            $subTemplateExpression-\u003edisplay();\n        }\n    }\n}\n```\n\n#### Miscellaneous\n\nTo determine wether you are inside/outside of a view helper you can use the static method `TPawl\\LiTE\\Context\\Context::isEmpty()`.\nIt returns `false` if you are inside a view helper, `true` otherwise.\n\nThere is the class `TPawl\\LiTE\\Version` defined, that holds version information for **LiTE**.\nIt has the following constants defined:\n* `MAJOR` for the major version number (major release).\n* `MINOR` for the minor version number (minor release).\n* `REVISION` for the revision number (patch level).\n\nExamples:\n\n```php\nuse TPawl\\LiTE\\PackageInformations;\n\necho 'Powered by ', PackageInformations::PACKAGE_NAME , ' ', PackageInformations::makePackageVersionString();\n\necho 'Copyright \u0026copy; ', PackageInformations::PACKAGE_COPYRIGHT['years'], ' by ', PackageInformations::makePackageCopyrightHoldersString();\n```\n\n### LiTE for Template Designers\n\n#### Template variables\n\n```\n\u003c?php $this-\u003efoo; ?\u003e\n```\n\nThis defines a template variable with name `foo`.\nThe complete expression above is replaced by the value of the template variable `foo`.\n\n#### View helpers\n\n```\n\u003c?php self::bar(); ?\u003e\n```\n\nThis calls the view helper `BarViewHelper` with no arguments.\nIf you have arguments, you have to write them as a comma separated list between the parentheses after `bar`.\nExample: `\u003c?php self::bar('arg1', 'arg2', ...); ?\u003e`\n\nThe complete expression above is replaced by the output of the view helper `BarViewHelper`.\n\n#### Predefined view helpers\n\nThere is the view helper `_xmlViewHelper` predefined.\nIt is called like this:\n\n```\n\u003c?php self::_xml('version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"'); ?\u003e\n```\n\nThis will convert to `\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?\u003e`.\nIt is useful if you want to output XML.\n\nExample\n-------\n\nSave the following view helper to a file named `MsgViewHelper.php` in any folder:\n\n```php\n\u003c?php\n\nclass MsgViewHelper implements TPawl\\LiTE\\ViewHelperInterface\n{\n    public static function execute(array $arguments): void\n    {\n        if (version_compare(PHP_VERSION, '7.1.0') \u003e= 0) {\n\n            $msg = 'Everything is fine';\n\n        } else {\n\n            $msg = 'This is not good';\n        }\n        print $msg;\n    }\n}\n\n```\n\nSuppose you have the following script in the same folder as the above view helper:\n\n```php\n\u003c?php\n\nrequire_once '/path/to/vendor/autoload.php';\n\n$template = \u003c\u003c\u003c'HTML'\n\u003c!DOCTYPE html\u003e\nHello \u003c?php $this-\u003ename; ?\u003e!\u003cbr\u003e\nYou are running PHP \u003c?php $this-\u003ever; ?\u003e: \u003c?php self::msg(); ?\u003e\nHTML;\n\n$variables = [\n    'name' =\u003e 'Thomas',\n    'ver' =\u003e PHP_VERSION,\n];\n\n$settings = [\n    $template,\n    $variables,\n    '.',\n    '',\n];\n\n$templateExpression = new TPawl\\LiTE\\Expressions\\TemplateExpression($settings);\n\n$templateExpression-\u003edisplay();\n\n```\n\nIf you call the above script in the browser, one possible output could be:\n\n```\nHello Thomas!\nYou are running PHP 7.1.10: Everything is fine\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpawl%2Flite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftpawl%2Flite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpawl%2Flite/lists"}