dsawardekar/wordpress-twig-helper

Helpers for using Twig with WordPress

0.2.0 2014-05-10 09:26 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:27:09 UTC


README

A tiny Library for using Twig Templates in WordPress Plugins & Themes.

Deprecated. This repo has been merged into Arrow.

Usage

The WpTwigHelper\TwigHelper is a helper class that provides the bulk of the functionality.

First give the TwigHelper a base directory. This will usually correspond to your plugin or theme directory. It must contain a templates sub directory that contains the .twig templates.

<?php use WpTwigHelper\TwigHelper;

$helper = new TwigHelper();
$helper->setBaseDir(plugin_dir_path(__FILE__));

Then if you have the template hello.twig in the templates directory with the contents,

Hello {{ name }}

You can render the template with the render method. It returns the output of rendering the twig template. Use the display method to echo the output instead.

<?php
$helper->render('hello', array('name' => 'there'));

Precompiling Templates

You can optionally precompile the templates using the provided command line tool, twig_compile. It takes the source directories to compile and the destination to put the compiled twig templates into.

$ twig_compile -s templates -t dist/templates

This command can be added to your build process.

By default the TwigHelper looks for compiled templates in the dist/templates directory. If present the Twig compilation phase is skipped. This will speed up rendering of your templates as it's equivalent to using inline PHP.

When dist/templates is absent caching is turned off instead.

System Requirements

  • Same as Twig, ie:- PHP 5.3.2+

License

MIT License. Copyright © 2014 Darshan Sawardekar.