A simple script that allows you to render Twig or plain PHP templates while manipulating sample data loaded from YAML models.
This can be useful for frontend developers that need to manipulate sample data within their templates before the template is used within a CMS. A backend developer may also provide realistic sample data to use in templating before even having the backend ready.
Just clone this repository or download as .zip.
The following structure is included:
/projectdirectory
/models
/templates
/templateloader
index.php
.htaccess
By default you can store your .html templates in /templates and your .yml models in /models. You can change these directories in /templateloader/config.php, or you can even use the root directory if you leave the corresponding config entries empty.
In /templateloader/config.php you can also change the "template_engine" to "php" if you don't like to use twig. The script will then look for .php templates instead of .html.
The idea is that If you navigate to
http://localhost/projectdirectory/index.php/sample
the script will load the templates/sample.html twig template with the data from models/sample.yml YAML model
http://localhost/projectdirectory/index.php/sample/foo/bar
will load templates/sample.html twig template with the data from models/foo.yml and models/bar.yml YAML model
You may have some YAML models that hold general data shared along all your templates (e.g. the navigation data, the site's name etc). To autoload this models without adding them as URL parameters edit /templateloader/config.php and add them in "autoload_models" like this:
'autoload_models' => array(
'common',
'header'
),
An .htaccess is included, so if you have apache with mod_rewrite enabled you can ommit the index.php part. For example:
http://localhost/projectdirectory/index.php/sample
can change to:
http://localhost/projectdirectory/sample
This script includes/uses the Twig library for template rendering and the Spyc library for YAML parsing.