Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/micropackage/templates
Simple PHP template engine which is easy to use
https://github.com/micropackage/templates
bracketspace composer-library micropackage template-engine wordpress
Last synced: about 1 month ago
JSON representation
Simple PHP template engine which is easy to use
- Host: GitHub
- URL: https://github.com/micropackage/templates
- Owner: micropackage
- License: gpl-3.0
- Created: 2020-01-30T13:53:36.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2021-11-15T21:12:42.000Z (about 3 years ago)
- Last Synced: 2024-11-17T02:38:20.745Z (about 2 months ago)
- Topics: bracketspace, composer-library, micropackage, template-engine, wordpress
- Language: PHP
- Size: 91.8 KB
- Stars: 30
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Templates
[![BracketSpace Micropackage](https://img.shields.io/badge/BracketSpace-Micropackage-brightgreen)](https://bracketspace.com)
[![Latest Stable Version](https://poser.pugx.org/micropackage/templates/v/stable)](https://packagist.org/packages/micropackage/templates)
[![PHP from Packagist](https://img.shields.io/packagist/php-v/micropackage/templates.svg)](https://packagist.org/packages/micropackage/templates)
[![Total Downloads](https://poser.pugx.org/micropackage/templates/downloads)](https://packagist.org/packages/micropackage/templates)
[![License](https://poser.pugx.org/micropackage/templates/license)](https://packagist.org/packages/micropackage/templates)
## 🧬 About Templates
Templates 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.
## 💾 Installation
``` bash
composer require micropackage/templates
```## 🕹 Usage
Let's assume your template tree looks like this:
```
my-plugin/
├── admin/
│ └── templates/
│ ├── notice.php
│ └── settings.php
└── frontend/
└── templates/
├── profile.php
└── welcome.php
```First, you need to define at least one template storage. In the above case we have two places with templates.
```php
Micropackage\Templates\Storage::add( 'admin', $plugin_dir . '/admin/templates' );
Micropackage\Templates\Storage::add( 'frontend', $plugin_dir . '/frontend/templates' );
```Then you can easily render template:
```php
$template = new Micropackage\Templates\Template( 'frontend', 'profile', [
'user_name' => $user_name,
'posts' => get_posts( [ 'author' => $user_id ] ),
] );$template->render();
```The template file could look like this:
```php
Howdy, the( 'user_name' ); ?>
See your posts:
- post_title; ?>
get( 'posts' ) as $post ) : ?>
```
### Accessing variables in the template file
In the template file, `$this` points to the template instance, which means you can access all the template methods.
The basic usage is:
```php
$this->the( 'var_name' ); // Prints the value.
$var_name = $this->get( 'var_name' ); // Gets the value.
```
But you can also use the shorthand closure methods:
```php
$the( 'var_name' ); // Prints the value.
$var_name = $get( 'var_name' ); // Gets the value.
```
### Default variable values
When variable is not defined, you can specify its default value:
```php
$the( 'var_name', 'Default val' );
$var_name = $get( 'var_name', 'Default val' );
```
### Available template methods
Template class methods.
| Method | Description | Returns |
| ----------------------------------------------- | --------------------------------- | ------------------------------------------------------------ |
| ```get_path()``` | Gets full path with extension | *(string)* |
| ```get_rel_path()``` | Gets relative path with extension | *(string)* |
| ```get_vars()``` | Gets all variables | *(array)* |
| ```clear_vars()``` | Clears all variables | `$this` |
| ```set((string) $var_name, (string) $value )``` | Sets the variable value | `$this` |
| ```get( (string) $var_name )``` | Gets the variable value | *(mixed\|null)*
Null if variable with given name wasn't set |
| ```the( (string) $var_name )``` | Prints the variable value | void |
| ```remove( (string) $var_name )``` | Removes the variable | `$this` |
| ```render()``` | Renders the template | void |
| ```output()``` | Outputs the template | *(string)* |
### Template constructor params
```php
$template = new Micropackage\Templates\Template(
$storage_name = 'frontend',
$template_name = 'profile',
$variables = [
'var_key' => $var_value,
]
);
```
| Parameter | Type | Description |
| -------------------- | ------------ | ------------------------------------------------------------ |
| ```$storage_name``` | **Required** | Must match registered storage |
| ```$template_name``` | **Required** | Relative template path, example:
`user/section/profile` will be resolved to:
`$storage_path . '/user/section/profile.php'` |
| ```$variables``` | Optional | Array of template variables in format:
`key => value`
Can be added later with `set()` method |
### Helper functions
You can use the procedural approach as well:
```php
// Print the template.
Micropackage\Templates\template( $storage_name, $template_name, $variables );
// Get the template output.
Micropackage\Templates\get_template( $storage_name, $template_name, $variables );
```
All the parameters remains the same as for the `Template` class.
## 📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by [BracketSpace](https://bracketspace.com).
## 📖 Changelog
[See the changelog file](./CHANGELOG.md).
## 📃 License
GNU General Public License (GPL) v3.0. See the [LICENSE](./LICENSE) file for more information.