Bare bones Twig templating support for WordPress
Sprig brings the Twig templating engine to WordPress. Install the plugin and get started separating your HTML from PHP.
Other Twig plugins like Timber try to bring lots of WordPress functionality into Twig. Sprig believes in separating PHP from HTML as much as possible. PHP is for gathering and massaging data and Sprig/Twig templates are for rendering HTML using the data passed to it. Keep things simple and only pass data to a template that the template needs to render.
- Clone this repo or download a zip
- Add this to the plugin directory of your WordPress site in
/wp-content/plugins/
- Run
composer install
to download the Twig template engine dependency
Create a directroy in your theme called views
or twig
to hold your Twig template files. Render your templates using the following methods passing an array of data to be used by the template.
Sprig::render()
will render a Twig template using an array of data and return it as a stringSprig::out()
willecho
a rendered templateSprig::do_action()
will capture the output of a WordPress action and return a string
example.php
<?php
$context = array(
'title' => 'Sprig is awesome!',
'url' => 'https://github.com/kingkool68/sprig/',
);
// Render the template and return a string
$thing = Sprig::render( 'example.twig', $context );
// Echo out the rendered template
Sprig::out( 'example.twig', $context );
example.twig
<p>
<a href="{{ url|esc_url }}">{{ title }}</a>
</p>
Output
<p>
<a href="https://github.com/kingkool68/sprig/">Sprig is awesome!</a>
</p>
I've put together a simple demo theme to show how Sprig can be used within a WordPress theme.
Twig filters allow a string to be manipulated in a Twig template. Most of the default filters in Sprig are WordPress' escaping functions for securing output as late as possible.
esc_attr
esc_html
esc_url
esc_js
esc_textarea
tag_escape
sanitize_email
sanitize_html_class
antispambot
wptexturize
absint
Additional Twig filters can be added via the WordPress filter sprig/twig/filters
and adding a callable function with a key to the array. This filter should be called before the init
action so Twig filters can be set up properly in time.
Example for adding the sanitize_title()
function as a Twig filter:
function filter_sprig_twig_filters( $filters = array() ) {
$filters['sanitize_title'] = 'sanitize_title';
return $filters;
}
add_filter( 'sprig/twig/filters', 'filter_sprig_twig_filters' );
Twig functions let you call PHP functions from within Twig templates. Sprig enables a handful of WordPress functions used in the base Twig template and WordPress' checked
/selected
/disabled
form helpers.
checked()
selected()
disabled()
wp_head()
wp_footer()
body_class()
get_header()
get_footer()
wp_title()
Additional Twig functions can be added via the WordPress filter sprig/twig/functions
and adding a callable function with a key to the array. This filter should be called before the init
action so Twig functions can be set up properly in time.
Example for adding the wp_nonce_field()
function as a Twig function:
function filter_sprig_twig_functions( $functions = array() ) {
$functions['wp_nonce_field'] = 'wp_nonce_field';
return $functions;
}
add_filter( 'sprig/twig/functions', 'filter_sprig_twig_functions' );
Sprig offers various WordPress filters to customize its behavior.
sprig/twig
for modifying Twig itselfsprig/twig/filters
for adding or removing available Twig filterssprig/twig/functions
for adding or removing available Twig functionssprig/roots
for modifying which directories Twig should look for twig templates insprig/theme_dirs
for modifying the name of directories to look for Twig templates in (Example: if you want to change the directory fromviews
ortwig
totemplates
)sprig/twig_loader
for modifying the Twig loader environment (see https://twig.symfony.com/doc/2.x/api.html)
PHP compatibility is dependent on Twig's PHP prerequisites. Use the appropriate branch of Sprig to meet your minimum PHP requirements.