There are dozens of templating engines out there, with options such as Smarty, Twig (used in the upcoming version of Drupal) and Blade (the default for Laravel) among the best known - as well as vanilla PHP, of course. Stepping away from PHP specifically, eRuby / ERB and Haml for Ruby / Ruby on Rails, and Javascript has scores of popular choices including Mustache, Handlebars, Hogan and EJS. Some have subtly different syntax, some more markedly so.
One which differs quite significantly from most is Jade, an engine usually associated with Javascript applications - it’s supported out-of-the-box by Express for Node.js, for example. It’s Jade I’m going to look at in this article; or more specifically the PHP port JadePHP.
Haml and Jade
It would be remiss to talk about Jade without mentioning Haml, from which Jade takes its inspiration - and indeed there are several libraries for using Haml with PHP. Jade shares its overall philosophy, which is to make templating “beautiful” and use what the authors describe as templating “haiku”. Whatever that actually means, there’s no denying Haml and Jade do share some characteristics which make them fundamentally different to most templating languages.
What’s the Difference?
Most templating engines involve writing the target markup and “injecting” it with placeholders and / or basic logic - a superset, in a sense. Jade still has placeholders and logic, but also provides a shorthand for writing XML-like elements. Generally that means html, although you can also use it for things like RSS as well as XML itself.
In fact if you wanted to, you could just use Jade as a shorthand for html without taking advantage of its more “traditional” templating features.
How to use the Repository
Rather frustratingly, the code is not currently available via Composer - although it should be a simple enough task to package it up, if anyone has an hour or two. You can get it to to work, however, by cloning the repository and include‘ing or require‘ing the included autoload.php.dist (the Github repository includes Symfony’s UniversalClassLoader).
Here’s an example, adapted from the one in the project’s README, which assumes that the repository has been downloaded into a directory called jade:
require('./jade/autoload.php.dist');
use Everzet\Jade\Dumper\PHPDumper,
Everzet\Jade\Visitor\AutotagsVisitor,
Everzet\Jade\Filter\JavaScriptFilter,
Everzet\Jade\Filter\CDATAFilter,
Everzet\Jade\Filter\PHPFilter,
Everzet\Jade\Filter\CSSFilter,
Everzet\Jade\Parser,
Everzet\Jade\Lexer\Lexer,
Everzet\Jade\Jade;
$dumper = new PHPDumper();
$dumper->registerVisitor('tag', new AutotagsVisitor());
$dumper->registerFilter('javascript', new JavaScriptFilter());
$dumper->registerFilter('cdata', new CDATAFilter());
$dumper->registerFilter('php', new PHPFilter());
$dumper->registerFilter('style', new CSSFilter());
// Initialize parser & Jade
$parser = new Parser(new Lexer());
$jade = new Jade($parser, $dumper);
$template = __DIR__ . '/template.jade';
// Parse a template (both string & file containers)
echo $jade->render($template);
This will compile the file template.jade and echo its contents.
Where you actually use this depends on your workflow, whether you’re using a framework, and so on. You could, perhaps, use a service such as Watchman, Guard or Resource Watcher to watch the filesystem for changes to your Jade templates, and compile them at the appropriate time during the development process.
Continue reading %Introduction to JadePHP%
more
{ 0 comments... » Introduction to JadePHP read them below or add one }
Post a Comment