ride/lib-template

Ride框架的模板库。

1.0.2 2016-10-07 12:21 UTC

This package is auto-updated.

Last update: 2024-09-13 00:07:06 UTC


README

PHP Ride框架的模板抽象库。

库中包含的内容

引擎

Engine的实现将通过Template接口实现实际的模板引擎。模板引擎决定了要写哪些特性和语法(或语言)。

EngineModel

EngineModel是可用模板引擎数据存储的接口。

主题

通过Theme接口可选地支持模板主题。每个主题都可以实现自己的模板版本。这使得返回相同数据的不同表示成为可能。

主题选择它可用的引擎。

您可以通过定义其父主题使主题分层。这使得创建子主题变得容易,并帮助引擎在资源找不到时回退。

ThemeModel

ThemeModel是可用主题数据存储的接口。

模板

Template实例定义了渲染模板的资源变量。使用ThemedTemplate,您可以在特定主题中指定一个模板。

资源应该是通用的,以便它不绑定到特定的模板引擎。例如:使用base/index而不是view/smarty/themes/bootstrap/base/index.tpl

代码示例

查看此代码示例以了解该库

<?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);
}

实现

有关更多示例,您可以查看以下库的实现

安装

您可以使用Composer来安装此库。

composer require ride/lib-template