TWIG with WordPress part1

Featured Image

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: http://inchoo.net
  Description: Engine for creating twig templates for wordpress
  Author: Darko GoleŇ°
  Author URI: http://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: http://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. :-)

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


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> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.