{"id":15578550,"url":"https://github.com/xp-forge/handlebars-templates","last_synced_at":"2025-06-27T05:35:27.943Z","repository":{"id":42446651,"uuid":"338553087","full_name":"xp-forge/handlebars-templates","owner":"xp-forge","description":"Handlebars templates for XP web frontends","archived":false,"fork":false,"pushed_at":"2024-04-12T09:17:33.000Z","size":104,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-12T23:02:12.476Z","etag":null,"topics":["assets","handlebars-helpers","handlebars-partials","handlebars-templates","php7","php8","yaml-frontmatter"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"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-02-13T10:52:08.000Z","updated_at":"2023-07-23T07:40:49.000Z","dependencies_parsed_at":"2023-11-19T10:28:07.476Z","dependency_job_id":"39df423f-54fd-4754-b957-a9e5b67235d7","html_url":"https://github.com/xp-forge/handlebars-templates","commit_stats":{"total_commits":66,"total_committers":1,"mean_commits":66.0,"dds":0.0,"last_synced_commit":"8ef7c0c1d190a3df23c5a5e1aaa619cecd4bdae5"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fhandlebars-templates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fhandlebars-templates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fhandlebars-templates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fhandlebars-templates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/handlebars-templates/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557798,"owners_count":20958047,"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":["assets","handlebars-helpers","handlebars-partials","handlebars-templates","php7","php8","yaml-frontmatter"],"created_at":"2024-10-02T19:11:26.151Z","updated_at":"2025-06-27T05:35:27.931Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Handlebars for XP web frontends\n===============================\n\n[![Build status on GitHub](https://github.com/xp-forge/handlebars-templates/workflows/Tests/badge.svg)](https://github.com/xp-forge/handlebars-templates/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/handlebars-templates/version.svg)](https://packagist.org/packages/xp-forge/handlebars-templates)\n\nHandlebars template engine implementation to be used in conjunction with [XP web frontends](https://github.com/xp-forge/frontend).\n\nExample\n-------\nWiring happens inside your web application:\n\n```php\nuse web\\frontend\\{Frontend, AssetsFrom, HandlersIn, Handlebars};\nuse web\\Application;\n\nclass App extends Application {\n\n  /** Returns routing for this web application */\n  public function routes() {\n    return [\n      '/static' =\u003e new AssetsFrom($this-\u003eenvironment-\u003epath('src/main/webapp')),\n      '/'       =\u003e new Frontend(\n        new HandlersIn('com.example.app.web'),\n        new Handlebars($this-\u003eenvironment-\u003epath('src/main/handlebars'))\n      )\n    ];\n  }\n}\n```\n\nTemplates\n---------\nThe templates live in `src/main/handlebars`, their names corresponding to lowercased version of the handlers' names (`Home::class` =\u003e `home.handlebars`).\n\nTemplates support YAML front matter, which can be used to set defaults for template globals. Example:\n\n```handlebars\n---\nnav:\n  /: Home\n  /about: About\n  /login: Login\n---\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e...\u003c/head\u003e\n  \u003cbody\u003e\n    \u003cnav\u003e\n      {{#each nav}}\n        \u003ca href=\"{{@key}}\"\u003e{{.}}\u003c/a\u003e\n      {{/each}}\n    \u003c/nav\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nFragments\n---------\nInstead of rendering an entire template, we can render special inline partials we call *fragments*. They are declared as follows:\n\n```handlebars\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e...\u003c/head\u003e\n  \u003cbody\u003e\n    {{#*fragment \"listing\"}}\n      \u003cul\u003e\n        {{#each items}}\n          \u003cli\u003e{{.}}\u003c/li\u003e\n        {{/each}}\n      \u003c/ul\u003e\n    {{/fragment}}\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n...and are rendered by selecting them via *fragment()* in our handler:\n\n```php\nuse web\\frontend\\{Handler, Get};\n\n#[Handler]\nclass Index {\n  private $list= ['One', 'Two', 'Three'];\n\n  #[Get]\n  public function index() {\n    return View::named('index')-\u003ewith(['list' =\u003e $this-\u003elist]);\n  }\n\n  #[Get('/listing')]\n  public function partial() {\n    return View::named('index')-\u003efragment('listing')-\u003ewith(['list' =\u003e $this-\u003elist]);\n  }\n}\n```\n\nAccessing the URI */listing* will render only the `\u003cul\u003e...\u003c/ul\u003e` instead of the entire document. These fragments can be used in conjunction with frameworks like [htmx](https://htmx.org/), see [this gist for an example](https://gist.github.com/thekid/019b12627f129afe7bdaea4cc594b29a).\n\nHelpers\n-------\nOn top of the [built-in functionality in Handlebars](https://github.com/xp-forge/handlebars), this library includes the following essential helpers:\n\n* `encode`: Performs URL-encoding \n* `equals`: Tests arguments for equality\n* `contains`: Tests whether a string or array contains a certain value\n* `size`: Returns string length or array size\n* `min`: Returns smallest element\n* `max`: Returns largest element\n* `any`: Test whether any of the given arguments is truthy\n* `none`: Test whether none of the given arguments is truthy\n* `all`: Test whether all of the given arguments is truthy\n\n### Date handling\n\n```php\nuse util\\TimeZone;\nuse web\\frontend\\Handlebars;\nuse web\\frontend\\helpers\\Dates;\n\nnew Handlebars($templates, [new Dates()]);\n\n// Pass timezone or NULL to use local timezone\nnew Handlebars($templates, [new Dates(new TimeZone('Europe/Berlin'))]);\nnew Handlebars($templates, [new Dates(null)]);\n\n// Pass default and named date format\nnew Handlebars($templates, [new Dates(null, [null =\u003e 'd.m.Y'])]);\nnew Handlebars($templates, [new Dates(null, ['us:short' =\u003e 'Y-m-d'])]);\n```\n\nThe `date` helper accepts anything the `util.Date` class accepts as constructor argument, or a `util.Date` instance itself. To format the date, the `format` argument can be used with anything the `util.Date::toString()` method accepts. Here are some examples:\n\n```handlebars\n{{date \"2021-02-13\"}}\n{{date \"13.02.2021 17:56:00\"}}\n{{date 1613209181}}\n{{date 1613209181279 timestamp=\"ms\"}}\n{{date created}}\n{{date created format=\"d.m.Y\"}}\n{{date created format=\"us:short\"}}\n{{date created timezone=\"America/New_York\"}}\n```\n\n### Logging\n\nThe `log` helper will echo the arguments passed to it:\n\n```handlebars\n{{log user}}\n{{log \"User profile:\" user}}\n```\n\nWhen using the development webserver, this shows the debug page:\n\n![Debug page](https://user-images.githubusercontent.com/696742/107873960-89cdc800-6eb6-11eb-954b-8b00324cce74.png)\n\nIn production environments, logs will end up on the server's standard output:\n\n![Console output](https://user-images.githubusercontent.com/696742/107874105-838c1b80-6eb7-11eb-8c7e-ee257ef1d92d.png)\n\n\nStandalone\n----------\nTo use the template engine by itself, simply instantiate and call its *render()* method:\n\n```php\nuse web\\frontend\\Handlebars;\n\n$engine= new Handlebars('.');\necho $engine-\u003erender('home', []);\n```\n\nThis will render the *home.handlebars* file and return the result as a string.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fhandlebars-templates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Fhandlebars-templates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fhandlebars-templates/lists"}