{"id":16380867,"url":"https://github.com/widoz/template-loader","last_synced_at":"2026-04-18T04:01:52.265Z","repository":{"id":57075722,"uuid":"78935713","full_name":"widoz/template-loader","owner":"widoz","description":"A WordPress template loader to use within your plugin","archived":false,"fork":false,"pushed_at":"2018-12-01T19:30:56.000Z","size":84,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-10-26T10:34:20.794Z","etag":null,"topics":["loader","oop","php","psr","template","template-loader","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/widoz.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.txt","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-01-14T11:15:11.000Z","updated_at":"2025-05-27T18:02:14.000Z","dependencies_parsed_at":"2022-08-24T14:55:46.278Z","dependency_job_id":null,"html_url":"https://github.com/widoz/template-loader","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/widoz/template-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widoz%2Ftemplate-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widoz%2Ftemplate-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widoz%2Ftemplate-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widoz%2Ftemplate-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/widoz","download_url":"https://codeload.github.com/widoz/template-loader/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widoz%2Ftemplate-loader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31955919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T00:39:45.007Z","status":"online","status_checked_at":"2026-04-18T02:00:07.018Z","response_time":103,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["loader","oop","php","psr","template","template-loader","wordpress"],"created_at":"2024-10-11T03:52:41.303Z","updated_at":"2026-04-18T04:01:52.223Z","avatar_url":"https://github.com/widoz.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/widoz/template-loader.svg?branch=master)](https://travis-ci.org/widoz/template-loader)\n[![codecov](https://codecov.io/gh/widoz/template-loader/branch/master/graph/badge.svg)](https://codecov.io/gh/widoz/template-loader)\n\n# WordPress Template Loader\n\nA simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.\n\nThis is not for the WordPress templates in the form of \"name-slug.php\" with fallback to name.php.\nIndeed this is a loader to build a basic data injection for views (templates).\n\n## Requirements\nPhp \u003e= 5.6.x\n\n## Examples\n\nThe `TemplateLoader\\Loader` class make use of the `Fluent` interface, so it's possible to concatenate\nthe calls to ask the instance to do the things.\n\n```php\n$loader = new TemplateLoader\\Loader('template_slug', new TemplateLoader\\DataStorage());\n\n$loader-\u003ewithData(new ModelInterface())\n       -\u003eusingTemplate('/relative/file/path.php')\n       -\u003erender()\n```\n\nThe class make use of WordPress function `locate_template` to locate the template file within the child and parent theme.\n\nIf you use the library within a plugin it's possible to define a fallback template part file path.\nAs the name *fallback* says the file will be loaded only in case nothing is found into the previous locations.\n\n```php\n$loader = new TemplateLoader\\Loader('template_slug', new TemplateLoader\\DataStorage());\n\n$loader-\u003ewithData(new ModelInterface())\n       -\u003eusingTemplate('/relative/file/path.php')\n       -\u003ebutFallbackToTemplate('/plugin/relative/file/path.php')\n       -\u003erender();\n```\n\nSince WordPress function `locate_template` allow to pass an array along a string, we can do the same by\npassing an array of template paths to the method `usingTemplate`.\n\n## Data Type\n\nThe data type used to inject values into the template is a class named `TemplateLoader\\ModelInterface`.\n`ModelInterface` doesn't declare any method. It's just a way to ensure the correct type of data is passed into the template loader.\n\nThis way we can extends the interface to create our own contracts based on the specific view.\n\n## Hooks\n\nThe `render` method, perform some filters that allow third party code to hook into the data and template\nto be modified before the template file is loaded.\n\nThere are two filter: `tmploader_template_engine_data` that is generic and pass the `$data` value and the `slug` property.\n\n```php\nadd_filter('tmploader_template_engine_data', function(TemplateLoader\\ModelInterface $data, string $slug) {\n\n    switch($slug) {\n        case 'my_slug':\n            // Do something\n        break;\n\n        default:\n        break;\n    }\n\n    return $data;\n\n});\n```\n\nThe second one is similar but the filter name include the slug template: `\"tmploader_template_engine_data_{$this-\u003eslug}\"`.\nThis in case you don't want or need to write conditional statements to know which is the current processing template.\n\n```php\nadd_filter('tmploader_template_engine_data_my_template', function(TemplateLoader\\ModelInterface $data) {\n    return new DataTemplate(); // An new instance of a class implementing TemplateLoader\\ModelInterface.\n});\n```\n\n## Performances\n\nSince it's usually to call the same view in different portion of the same page, so whitin the same\nhttp request, to prevent to access multiple time to the file system only to know where the file template\nis located, we defined an internal collection that can be used to store the template file paths.\n\nThe second time we try to ask the same template, we'll not perform any additional filesystem access in order\nto load the template file. This improve speed for multiple calls. Also since we don't create a strict relation\nbetwee the template and the data to inject, we can pass every time a different data value.\n\nSo, just to clarify with an example, during the same call we can instantiate the loader once and than ask to\nload the same template multiple times with different data values.\n\n```php\n$loader = new TemplateLoader\\Loader('template_slug', new TemplateLoader\\DataStorage());\n\n$loader-\u003ewithData(new ModelInterface())\n       -\u003eusingTemplate('/relative/file/path.php')\n       -\u003ebutFallbackToTemplate('/plugin/relative/file/path.php')\n       -\u003erender();\n\n// Some code ... and then ...\n\n$loader-\u003ewithData(new ModelInterface())\n       -\u003erender();\n```\n\nThe second time we call the `render` method we had changed only the data used within the template since\nwe have stored the template path related with the `slug` of the template. In this case `template_slug`.\n\nAvoiding unnecessary filesystem access that we know are slows.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidoz%2Ftemplate-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwidoz%2Ftemplate-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidoz%2Ftemplate-loader/lists"}