{"id":21989204,"url":"https://github.com/micropackage/templates","last_synced_at":"2025-04-30T11:30:43.425Z","repository":{"id":52666587,"uuid":"237223561","full_name":"micropackage/templates","owner":"micropackage","description":"Simple PHP template engine which is easy to use","archived":false,"fork":false,"pushed_at":"2021-11-15T21:12:42.000Z","size":94,"stargazers_count":30,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"develop","last_synced_at":"2024-11-17T02:38:20.745Z","etag":null,"topics":["bracketspace","composer-library","micropackage","template-engine","wordpress"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/micropackage.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":"2020-01-30T13:53:36.000Z","updated_at":"2024-06-20T10:46:51.000Z","dependencies_parsed_at":"2022-08-22T03:21:01.331Z","dependency_job_id":null,"html_url":"https://github.com/micropackage/templates","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Ftemplates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Ftemplates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Ftemplates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Ftemplates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/micropackage","download_url":"https://codeload.github.com/micropackage/templates/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227198221,"owners_count":17746507,"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":["bracketspace","composer-library","micropackage","template-engine","wordpress"],"created_at":"2024-11-29T19:28:21.308Z","updated_at":"2024-11-29T19:28:22.054Z","avatar_url":"https://github.com/micropackage.png","language":"PHP","readme":"# Templates\n\n[![BracketSpace Micropackage](https://img.shields.io/badge/BracketSpace-Micropackage-brightgreen)](https://bracketspace.com)\n[![Latest Stable Version](https://poser.pugx.org/micropackage/templates/v/stable)](https://packagist.org/packages/micropackage/templates)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/micropackage/templates.svg)](https://packagist.org/packages/micropackage/templates)\n[![Total Downloads](https://poser.pugx.org/micropackage/templates/downloads)](https://packagist.org/packages/micropackage/templates)\n[![License](https://poser.pugx.org/micropackage/templates/license)](https://packagist.org/packages/micropackage/templates)\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://bracketspace.com/extras/micropackage/micropackage-small.png\" alt=\"Micropackage logo\"/\u003e\n\u003c/p\u003e\n\n## 🧬 About Templates\n\nTemplates micropackage is very simple WordPress template engine with multi-storage support. The templates are not parsed or cached like Blade or Twig templates. It's just good ol' file loader with variable support.\n\n## 💾 Installation\n\n``` bash\ncomposer require micropackage/templates\n```\n\n## 🕹 Usage\n\nLet's assume your template tree looks like this:\n\n```\nmy-plugin/\n├── admin/\n│   └── templates/\n│      ├── notice.php\n│      └── settings.php\n└── frontend/\n    └── templates/\n       ├── profile.php\n       └── welcome.php\n```\n\nFirst, you need to define at least one template storage. In the above case we have two places with templates.\n\n```php\nMicropackage\\Templates\\Storage::add( 'admin', $plugin_dir . '/admin/templates' );\nMicropackage\\Templates\\Storage::add( 'frontend', $plugin_dir . '/frontend/templates' );\n```\n\nThen you can easily render template:\n\n```php\n$template = new Micropackage\\Templates\\Template( 'frontend', 'profile', [\n\t'user_name' =\u003e $user_name,\n\t'posts'     =\u003e get_posts( [ 'author' =\u003e $user_id ] ),\n] );\n\n$template-\u003erender();\n```\n\nThe template file could look like this:\n\n```php\n\u003cp\u003eHowdy, \u003c?php $this-\u003ethe( 'user_name' ); ?\u003e\u003c/p\u003e\n\n\u003cp\u003eSee your posts:\u003c/p\u003e\n\n\u003cul\u003e\n\t\u003c?php foreach ( $this-\u003eget( 'posts' ) as $post ) : ?\u003e\n\t\t\u003cli\u003e\u003c?php echo $post-\u003epost_title; ?\u003e\u003c/li\u003e\n\t\u003c?php endforeach; ?\u003e\n\u003c/ul\u003e\n```\n\n### Accessing variables in the template file\n\nIn the template file, `$this` points to the template instance, which means you can access all the template methods.\n\nThe basic usage is:\n\n```php\n$this-\u003ethe( 'var_name' ); // Prints the value.\n$var_name = $this-\u003eget( 'var_name' ); // Gets the value.\n```\n\nBut you can also use the shorthand closure methods:\n\n```php\n$the( 'var_name' ); // Prints the value.\n$var_name = $get( 'var_name' ); // Gets the value.\n```\n\n### Default variable values\n\nWhen variable is not defined, you can specify its default value:\n\n```php\n$the( 'var_name', 'Default val' );\n$var_name = $get( 'var_name', 'Default val' );\n```\n\n### Available template methods\n\nTemplate class methods.\n\n| Method                                          | Description                       | Returns                                                      |\n| ----------------------------------------------- | --------------------------------- | ------------------------------------------------------------ |\n| ```get_path()```                                | Gets full path with extension     | *(string)*                                                   |\n| ```get_rel_path()```                            | Gets relative path with extension | *(string)*                                                   |\n| ```get_vars()```                                | Gets all variables                | *(array)*                                                    |\n| ```clear_vars()```                              | Clears all variables              | `$this`                                                      |\n| ```set((string) $var_name, (string) $value )``` | Sets the variable value           | `$this`                                                      |\n| ```get( (string) $var_name )```                 | Gets the variable value           | *(mixed\\|null)*\u003cbr /\u003eNull if variable with given name wasn't set |\n| ```the( (string) $var_name )```                 | Prints the variable value         | void                                                         |\n| ```remove( (string) $var_name )```              | Removes the variable              | `$this`                                                      |\n| ```render()```                                  | Renders the template              | void                                                         |\n| ```output()```                                  | Outputs the template              | *(string)*                                                   |\n\n### Template constructor params\n\n```php\n$template = new Micropackage\\Templates\\Template(\n\t$storage_name = 'frontend',\n\t$template_name = 'profile',\n\t$variables  = [\n\t\t'var_key' =\u003e $var_value,\n\t]\n);\n```\n\n| Parameter            | Type         | Description                                                  |\n| -------------------- | ------------ | ------------------------------------------------------------ |\n| ```$storage_name```  | **Required** | Must match registered storage                                |\n| ```$template_name``` | **Required** | Relative template path, example:\u003cbr /\u003e`user/section/profile` will be resolved to:\u003cbr /\u003e`$storage_path . '/user/section/profile.php'` |\n| ```$variables```     | Optional     | Array of template variables in format:\u003cbr /\u003e`key =\u003e value`\u003cbr /\u003eCan be added later with `set()` method |\n\n### Helper functions\n\nYou can use the procedural approach as well:\n\n```php\n// Print the template.\nMicropackage\\Templates\\template( $storage_name, $template_name, $variables );\n\n// Get the template output.\nMicropackage\\Templates\\get_template( $storage_name, $template_name, $variables );\n```\n\nAll the parameters remains the same as for the `Template` class.\n\n## 📦 About the Micropackage project\n\nMicropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.\n\nThe aim is to have multiple packages which can be put together to create something bigger by defining only the structure.\n\nMicropackages are maintained by [BracketSpace](https://bracketspace.com).\n\n## 📖 Changelog\n\n[See the changelog file](./CHANGELOG.md).\n\n## 📃 License\n\nGNU General Public License (GPL) v3.0. See the [LICENSE](./LICENSE) file for more information.\n","funding_links":[],"categories":["📃 About Template"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropackage%2Ftemplates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicropackage%2Ftemplates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropackage%2Ftemplates/lists"}