phpixie/template

PHPixie 的模板库

3.2.4 2018-02-16 00:37 UTC

This package is auto-updated.

Last update: 2024-09-13 01:28:36 UTC


README

Build Status Test Coverage Code Climate HHVM Status

Author Source Code Software License Total Downloads

PHPixie Template 使用 PHP 作为模板语言,但也可以处理布局、内容块、自定义扩展甚至自定义格式。使用它解析 HAML、Markdown 或您喜欢的任何其他格式都非常简单。您需要做的就是提供一个编译器,它将您的格式转换为普通的 PHP 模板,库将自行处理缓存结果、在文件修改时更新等。

继承
理解模板继承非常直观,尤其是如果您使用过 Twig 或 Smarty。以下是一个快速示例

<!--layout.php-->
<html>
    <title>
        <?php $this->block('title'); ?>
    </title>
    <body>
        <?php $this->childContent(); ?>
        
    </body>
</html>
``````php
<!--fairy.php-->
<?php $this->layout('layout'); ?>
<?php $this->startBlock('title'); ?>
Fairy page
<?php $this->endBlock(); ?>

<h2>Hello <?=$_($name) ?></h2>

现在让我们来渲染它

echo $template->render('fairy', array('name' => 'Pixie'));
<html>
    <title>Fairy page</title>
    <body>
        <h2>Hello Pixie</h2>
    </body>
</html>

您还可以包含一个子模板来渲染部分内容

include $this->resolve('fairy');

模板名称解析
通常,模板库有一些提供回退模板的方法,这些模板在您想要使用的模板不存在时使用。通常它们通过某种命名约定来处理。PHPixie 允许您使用 3 种定位器类型来微调名称解析

  • 目录 – 将模板名称映射到文件夹位置,最简单的一种
  • 组 – 允许您指定一组定位器。模板将在这些定位器中逐个搜索,直到找到。这用于提供回退
  • 前缀 – 允许您根据前缀来路由解析

这听起来比实际情况要复杂,让我们看看一个例子,假设以下配置

$config = $slice->arrayData([
    'resolver' => [
        'locator' => [
            'type' => 'prefix',
            'locators' => [
                
                'Site' => [
                    'directory' => __DIR__.'/site/',
                ],
                    
                'Theme' => [
                    'type' => 'group',
                    'locators' => [
                        [
                            'directory' => __DIR__.'/templates/',
                        ],
                        
                        [
                            'directory' => __DIR__.'/fallback/',
                        ],
                    ] 
                ]
            ]
        ]
    ]
]);

这意味着 Site::layout 模板将在 site/ 文件夹中搜索,而 Theme::home 一个将在 templates/fallback/ 中搜索。

当使用 PHPixie 框架时,您在 templateLocator.php 配置文件中定义您的定位器。您的定位器将使用您的包名称作为前缀。

扩展

扩展提供您可以在视图中使用的方法,它们对于格式化和转义等操作很有帮助。例如,让我们看看 HTML 扩展

class HTML implements \PHPixie\Template\Extensions\Extension
{
    public function name()
    {
        return 'html';
    }
    
    //Methods we would like to expose
    public function methods()
    {
        return array('escape', 'output');
    }
    
    //Also we can assign aliases to some methods, meaning that they will also
    //be assigned to a specified local variable
    //In this case it allows us to use $_($name) instead of $this->escape($name)
    public function aliases()
    {
        return array(
            '_' => 'escape'
        );
    }
    
    public function escape($string)
    {
        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
    }
    
    public function output($string)
    {
        echo $this->escape($string);
    }
}

然后您必须像格式一样在库构造函数中注入您的扩展。

创建自定义格式

所以让我们尝试将其与 Markdown 集成,我们将使用 mthaml/mthaml 来实现这一点

//composer.json
{
    "require": {
        "phpixie/template": "3.*@dev",
        "phpixie/slice": "3.*@dev",
        "mthaml/mthaml": "1.7.0"
    }
}

这是我们的编译器

class HamlFormat implements \PHPixie\Template\Formats\Format
{
    protected $mtHaml;
    
    public function __construct()
    {
        $this->mtHaml = new \MtHaml\Environment('php');
    }
    
    public function handledExtensions()
    {
        return array('haml'); // register which file extension we handle
    }
    
    public function compile($file)
    {
        $contents = file_get_contents($file);
        return $this->mtHaml->compileString($contents, $file);
    }
}

现在让我们将其注入到模板中

$slice = new \PHPixie\Slice();

$config = $slice->arrayData(array(
    'resolver' => array(
        'locator' => array(
            //template directory
            'directory' => __DIR__.'/templates/',
            'defaultExtension' => 'haml',
        )
    ),
    'compiler' => array(
        'cacheDirectory' => > __DIR__.'/cache/',
    )
));

$template = new \PHPixie\Template($slice, $config, array(), array(
    new HamlCompiler()
));

就这样,我们现在可以使用 HAML 作为模板,同时保留所有原始功能,如扩展和继承。