Thursday, November 20, 2014

Getting Started with FigDice

Amongst the many templating systems out there, most work in pretty much the same way; variables are “injected” using some syntax or another, be it curly braces, percentage signs or whatever that library’s convention happens to be. They’ll usually have basic control structures, such as if...then and, of course, iteration.


FigDice, however, takes an altogether different approach. Inspired by PHPTAL - the subject of a future article - it gives the view layer the reponsibility of “pulling” in the data it requires, rather than relying on controllers to assemble and “push” it into the templates.


In this two-part series, I’m going to take a more detailed look at it, at how it works and how to use it.


Installation


You can get the code from the website, Github or, better still, Composer:



"figdice/figdice": "dev-master"


The library is also available as a .phar; you can download the latest version (direct link) or generate it yourself using figdice-make-phar.php, available in the Github repository.


Basic Usage


Let’s first look at FigDice at its most basic - creating and rendering a view.


To begin, create a new instance of the view:



$view = new \figdice\View();


Obviously, you’ll need to ensure the library is available - using Composer provides a suitable autoloader.


Next, you need to load in the appropriate template, relative to the current path:



$view->loadFile( '../templates/home.html' );


To render the template, call render():



$view->render(); // returns the HTML content


With that in mind, let’s start building some templates.


Continue reading %Getting Started with FigDice%




by Lukas White via SitePoint

No comments:

Post a Comment