micropackage / templates
Simple PHP template engine which is easy to use
Installs: 18 609
Dependents: 0
Suggesters: 0
Security: 0
Stars: 30
Watchers: 4
Forks: 5
Open Issues: 0
Requires
- php: >=5.6
- micropackage/filesystem: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-16 04:16:52 UTC
README
🧬 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
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.
Micropackage\Templates\Storage::add( 'admin', $plugin_dir . '/admin/templates' ); Micropackage\Templates\Storage::add( 'frontend', $plugin_dir . '/frontend/templates' );
Then you can easily render template:
$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:
<p>Howdy, <?php $this->the( 'user_name' ); ?></p> <p>See your posts:</p> <ul> <?php foreach ( $this->get( 'posts' ) as $post ) : ?> <li><?php echo $post->post_title; ?></li> <?php endforeach; ?> </ul>
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:
$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:
$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:
$the( 'var_name', 'Default val' ); $var_name = $get( 'var_name', 'Default val' );
Available template methods
Template class methods.
Template constructor params
$template = new Micropackage\Templates\Template( $storage_name = 'frontend', $template_name = 'profile', $variables = [ 'var_key' => $var_value, ] );
Helper functions
You can use the procedural approach as well:
// 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.
📖 Changelog
📃 License
GNU General Public License (GPL) v3.0. See the LICENSE file for more information.