TWIG with WordPress part1

TWIG with WordPress part1

Making TWIG autoload from WP plugin

Maybe someone will say: this is a crazy idea. Why would someone want to do that?
Maybe it really is crazy, but I would say: why not?

What is TWIG? TWIG is new generation php templating engine made by Sensio Labs. It is used in their new project Symfony2 as main templating engine provided with framework.
As official page says:

Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.
It is not necessary to explain in detail why will someone (me) want to use that as wordpress templating engine, but one of the main reasons is that I want to rewrite some messed up php template for WP to better code organized one, and at the and I want it to be with cleaner code as possible.
I don’t like very much when is necessary to mix up php tags and functions inside HTML code to make it work.
After some thinking about TWIG and how to implement it in my WP installation I got idea:
I will make plug in for WP that will provide all necessary stuff to start using TWIG with WP.

First I created the plugin folder named wp_twig_engine.I put folder named ‘lib‘ downloaded from twig-project site in there. So I had the twig library almost prepared for using.
Now I had to find some appropriate wordpress hook to inject my code into wordpress application logic. After some investigating and testing, I found that it will be possible to use the ‘init‘ hook and register autoloaders for TWIG library and my custom class in the same function, so I can reach my code from inside template folder later.

Main plugin file: wp-twig.php

<?php
 
/*
  Plugin Name: WordPress Twig templating engine
  Version: 1.0
  Plugin URI: https://inchoo.net
  Description: Engine for creating twig templates for wordpress
  Author: Darko Goleš
  Author URI: https://inchoo.net/author/darko.goles/
 */
//Main twig library autoloader file
require_once dirname(__FILE__) . '/lib/Twig/Autoloader.php';
//My custom class made, I also want it to be autoloaded
require_once dirname(__FILE__) . '/Wp_TwigEngine.php';
 
function twigAutoLoad() {
 
    Twig_Autoloader::register();
    Wp_TwigEngine_Autoloader::register();
}
 
add_action('init', 'twigAutoLoad');
?>

Just to show you how to autoload my custom class with that (of course I got idea from original twig autoloader and just changed the code to fit my needs:

<?php
/*
Wp_TwigEngine.php
  Author: Darko Goleš
  Author URI: https://inchoo.net/author/darko.goles/
*/
class Wp_TwigEngine_Autoloader {
 
    static public function register() {
        ini_set('unserialize_callback_func', 'spl_autoload_call');
        spl_autoload_register(array(new self, 'autoload'));
    }
 
    static public function autoload($class) {
        if (0 !== strpos($class, 'Wp_TwigEngine')) {
            return;
        }
 
        if (file_exists($file = dirname(__FILE__) . '/../' . str_replace(array('_', "\0"), array('/', ''), $class) . '.php')) {
            echo($file);
            exit;
            require $file;
        }
    }
 
}

And that’s it from the plugin side for now. Just have to implement it in the template folder. That is additional theme for another article. 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Symfony templating with Twig Zoran Salamun
Zoran Salamun, | 0

Symfony templating with Twig

TWIG with wordpress part2 Darko Goles
Darko Goles, | 9

TWIG with wordpress part2

Symfony 2 paginator – improved version Darko Goles
Darko Goles, | 0

Symfony 2 paginator – improved version

1 comment

  1. Could you guys make a sequel to this article, with composer + wordpress combo.
    It seems that the autoloader will be gone in twig 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.