All Projects → micropackage → Templates

micropackage / Templates

Licence: gpl-3.0
Simple PHP template engine which is easy to use

Projects that are alternatives of or similar to Templates

Raccoon Plugin
With Raccoon, use a JSON or YAML file to manage WordPress theme features
Stars: ✭ 18 (-30.77%)
Mutual labels:  wordpress
Dashboard Cleanup
Cleans up the WP Admin backend by disabling various core WP and WC bloat features including Automattic spam, nag notices, tracking, and other items.
Stars: ✭ 23 (-11.54%)
Mutual labels:  wordpress
Cyr2lat
Converts Cyrillic characters in post, page and term slugs to Latin characters. Useful for creating human-readable URLs.
Stars: ✭ 25 (-3.85%)
Mutual labels:  wordpress
Wp Vuejs
WordPress VueJS Starter Theme
Stars: ✭ 19 (-26.92%)
Mutual labels:  wordpress
Press Sync
The easiest way to synchronize posts, media, users and more between two WordPress sites.
Stars: ✭ 22 (-15.38%)
Mutual labels:  wordpress
Wp Multitenancy Boilerplate
WordPress multitenancy boilerplate configured and managed with Composer and PHP dotenv.
Stars: ✭ 24 (-7.69%)
Mutual labels:  wordpress
Wordpress Theme Framework
Lightweight MVC theming framework for developers who want to better organize their own custom themes with an MVC approach.
Stars: ✭ 18 (-30.77%)
Mutual labels:  wordpress
Wp Controllers
The OOP Developer's best friend for working with objects in WordPress
Stars: ✭ 25 (-3.85%)
Mutual labels:  wordpress
Pdpa Consent
WordPress plugin for PDPA Consent allows you to notify to the user to accept privacy terms. Comply with Thailand PDPA law.
Stars: ✭ 23 (-11.54%)
Mutual labels:  wordpress
Wordprismic
Utility to import existing Wordpress blogs into the Prismic.io content platform
Stars: ✭ 25 (-3.85%)
Mutual labels:  wordpress
React email editor
This project is experimental! It's my attempt to create visual email template editor using React+Redux+etc... tools stack.
Stars: ✭ 19 (-26.92%)
Mutual labels:  template-engine
Ddev
DDEV-Local: a local PHP development environment system
Stars: ✭ 915 (+3419.23%)
Mutual labels:  wordpress
Mcavoy
Discover what visitors are searching for on your WordPress site.
Stars: ✭ 24 (-7.69%)
Mutual labels:  wordpress
Netlify Rebuild
WordPress Plugin to trigger Netlify rebuild
Stars: ✭ 19 (-26.92%)
Mutual labels:  wordpress
Maps Block Apple
An Apple Maps block for the WordPress block editor (Gutenberg).
Stars: ✭ 25 (-3.85%)
Mutual labels:  wordpress
Ng Wordpress Theme
Angular starter theme for WordPress
Stars: ✭ 18 (-30.77%)
Mutual labels:  wordpress
Wordpressplugin
Rocket.Chat.Livechat plug-in for WordPress
Stars: ✭ 23 (-11.54%)
Mutual labels:  wordpress
Wp Posts To Posts
Efficient many-to-many connections between posts, pages, custom post types, users.
Stars: ✭ 933 (+3488.46%)
Mutual labels:  wordpress
Genesis Simple Hook Guide
WordPress plugin that displays names of all Genesis hooks on the current page dynamically.
Stars: ✭ 25 (-3.85%)
Mutual labels:  wordpress
Space Lover
Git-ified. Synced via git-svn. Magically add an extra space between Chinese characters and English letters / numbers / common punctuation marks
Stars: ✭ 24 (-7.69%)
Mutual labels:  wordpress

Templates

BracketSpace Micropackage Latest Stable Version PHP from Packagist Total Downloads License

Micropackage logo

🧬 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.

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

$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:

// 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

See the changelog file.

📃 License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].