ride/lib-template

Template library of the Ride framework.

1.0.2 2016-10-07 12:21 UTC

This package is auto-updated.

Last update: 2024-12-13 00:53:44 UTC


README

Template abstraction library of the PHP Ride framework.

What's In The Library

Engine

The implementation of Engine will implement the actual template engine through the Template interface. A template engine decides the features and the syntax (or language) to write in.

EngineModel

An EngineModel is an interface for the data store of the available template engines.

Theme

Optional support for template themes through the Theme interface. Each theme can implement it's own version of a template. This makes it possible to return a different representation of the same data.

A theme chooses the engines it's available in.

You can make a theme hierarchical by defining it's parent. This makes it easy to create subthemes and helps the engine to fallback when a resource could not be found.

ThemeModel

A ThemeModel is an interface for the data store of the available themes.

Template

A Template instance defines the resource and the variables to render the template. With a ThemedTemplate, you can specify a template in a specific theme.

The resource should be generic so it's not bound to a specific template engine. For example: use base/index instead of view/smarty/themes/bootstrap/base/index.tpl.

Code Sample

Check this code sample to get an idea of this library:

<?php

use ride\library\template\engine\EngineModel;
use ride\library\template\engine\ThemeModel;
use ride\library\template\TemplateFacade;

function foo(EngineModel $engineModel, ThemeModel $themeModel) {
    $resource = 'path/to/resource';
    $variables = array('var1' => 'value1');

    // template facade should be made available by your implementation
    $templateFacade = TemplateFacade($engineModel, $themeModel);
    $templateFacade->setDefaultEngine('smarty');
    $templateFacade->setDefaultTheme('my-theme');

    // a simple template rendering
    $template = $templateFacade->createTemplate($resource, $variables);
    $template->set('var2', 'value2');

    $output = $templateFacade->render($template);

    // a template for a non-default theme
    $template = $templateFacade->createTemplate($resource, $variables, 'overriden-theme');

    // a template for a non-default theme with a specific engine
    $template = $templateFacade->createTemplate($resource, $variables, 'overriden-theme', 'my-engine');

    // get the file representation of a specific template
    $file = $templateFacade->getFile($template); // ride\library\system\file\File

    // get's the available template for a specific namespace
    $engine = null;
    $theme = null;
    $templates = $templateFacade->getFiles('path/to', $engine, $theme);
    echo $engine; // smarty
    echo $theme; // my-theme

    // reads the meta from the template comments in the beginning of the resource
    // syntax: [key: value([; key: value])*]
    // eg for Smarty: {* name: My Title; action: index *}
    // will return array('name' => 'My Title', 'action' => 'index')
    $meta = $templateFacade->getTemplateMeta($template);
}

Implementations

For more examples, you can check the following implementations of this library:

Installation

You can use Composer to install this library.

composer require ride/lib-template