net_bazzline/php_component_template

免费如自由的开源PHP模板引擎

3.1.3 2016-01-31 00:44 UTC

This package is auto-updated.

Last update: 2024-09-16 20:35:27 UTC


README

本项目旨在提供一个易于使用、免费且快速的PHP模板引擎(代号:yepte - 另一个PHP模板引擎)。

当前主分支的构建状态由Travis CI跟踪:Build Status Latest stable

scrutinizer的状态如下:code quality

versioneye的状态如下:Dependency Status

请查看openhub.net

原因

我想创建一个精简(从代码行数和文件数量来说)、快速、可扩展但可扩展的PHP模板引擎。本项目并不旨在动摇现有大型PHP模板引擎的地位。它们有不同的目标、更多的人力和不同的理念。

我个人喜欢php-text-template,但Sebastian的想法不同(写入文件)。将我的目标添加到他的项目中会给他库增加更多复杂性。

可用的模板

目前,此组件试图解决处理PHP模板时的三个问题。所有模板都是可堆叠的,这意味着你可以将一个模板键分配给另一个模板实例。

RuntimeContentBasedTemplate解决了在字符串中存储的内容替换问题。

FileBasedTemplate解决了在文件中存储的内容替换问题。

ComplexFileBasedTemplate解决了在文件中存储的复杂内容替换问题。这通常被称为PHP框架中的视图。

CallableComplexFileBasedTemplateManager解决了外部化可重用模板任务的问题。这通常被称为视图助手模式。

注意

什么是复杂内容?

复杂内容包含如下的代码:

$isFoo = ($bar === 'foo');
if ($isFoo) {
     /* ... */ 
} else { 
    /*  ... something else */ 
}

什么是可调用的?

可调用的就是著名的视图助手设计模式。模板提供了一个名为“registerCallable”的方法来注册一个可调用的,并将其绑定到一个名称。

//assuming the file in the relative path 'template.phtml' has the following content
//<?php $this->foobar('foo', 'bar');
$myViewHelper = function($foo, $bar) {
    return 'there is no ' . $foo . ' without a ' . $bar;
}
$template = new CallableComplexFileBasedTemplateManager('template.phtml');
$template->registerCallable('foobar', $myViewHelper);
echo $template->render() . PHP_EOL;
//expected result: there is no foo without a bar

我应该使用哪种类型的复杂内容?

这完全取决于你,代码相当灵活。

我的观点是,限制自己使用foreach。使用if/else是向“在模板中添加业务逻辑”迈出的一步。switch是朝这个方向迈出的另一步。

使用方法

use Net\Bazzline\Component\Template\RuntimeContentBasedTemplate;

//create a instance
$template = new RuntimeContentBasedTemplate();

//set content
$template->setContent('there is no {one} without a {two}');

//assign variable one by one ...
$template->assignOne('one', 'foo');
//... or by providing an array
$template->assignMany(array('one' => 'foo'));
//you can also assign a template to a template
//  this is used if a layout template is defined with a {content} key for e.g.
$template->assignOne('content', $otherTemplate);

//you can render it in different ways
//1) explicit calling the method
echo $template->render();
//2) casting it to a string
echo (string) $template;
//3) using it as a function
//  you can also provide all optional parameters like in the constructor
echo $template();

安装

手动

mkdir -p vendor/net_bazzline/php_component_template
cd vendor/net_bazzline/php_component_template
git clone https://github.com/bazzline/php_component_template .

使用Packagist

composer require net_bazzline/php_component_template:dev-master

API

API可在bazzline.net找到。

历史

  • 即将推出
    • @todo
      • 添加示例
      • 添加按月下载图标
      • 如有需要,添加拒绝/接受/辞职功能
      • 添加单元测试
      • 实现缓存
      • 添加对不同模板扩展的支持
      • 重构内部结构,用不可变领域对象数组替换数组数据
    • 将测试自动加载移动到合适的配置中
    • zend-expressive-template适配器代码移动到自己的仓库
    • 移除了TryToInstallZendExpressiveTemplate命令
    • 移除了zend-expressive-template作为建议的包
    • 重构了composer.json
  • 3.1.3 - 发布于2016年1月31日
    • 添加了zend-expressive-template作为建议的包
    • 添加了TryToInstallZendExpressiveTemplate命令,在开发模式下如果满足要求则安装zend-expressive-template
    • 增加了对php 7的支持
    • 增加了将模板堆叠到模板中的测试
    • 迁移到psr-4自动加载
    • 移除了对php 5.3.3的支持
  • 3.1.2 - 发布于2016年1月26日
    • 更新了依赖项
  • 3.1.1 - 发布于2015年12月11日
    • 重构了CallableComplexFileBasedTemplateManager::registerCallable()
    • 更新了依赖项
  • 3.1.0 - 发布于2015年10月28日
  • 3.0.0 - 发布于2015年10月9日
    • 添加了travis、scrutinizer、openhub、versioneye的链接
    • 添加了DelimiterInterface
    • 在所有模板中实现了__invoke以提高使用和渲染速度
    • 实现了reset
    • 通过引入AbstractFileBasedTemplate和移除分隔符处理来重构现有模板
    • setOpenDelimiter重命名为setOpeningDelimiter
    • FileTemplate重命名为FileBasedTemplate
    • StringTemplate重命名为RuntimeContentBasedTemplate
    • ViewTemplate重命名为ComplexFileBasedTemplate
    • RuntimeContentBasedTemplateFileBasedContent的构造函数中调整了filePathvariables的顺序
    • 开始链接部分
    • 通过在extract方法中添加"EXTR_SKIP"来更新ComplexFileBasedTemplate变量处理
    • 更新了依赖项
  • 2.1.0 - 发布于2015年10月6日
    • __toString()方法描述中添加了throws RuntimeException
    • 添加了ViewTemplate
    • 添加了isAssigned
  • 2.0.0 - 发布于2015年10月2日
    • 添加了TemplateInterface
    • 将基于文件的模板与通用模板解耦
      • 将类Template重命名为AbstractTemplate
      • 引入了 FileTemplateStringTemplate
  • 1.1.0 - 发布于 2015年10月1日
    • 修复了 "assignOne" 中的主要错误(现在它按预期工作)
  • 1.0.0 - 发布于 2015年10月1日
    • 初始版本

其他库的链接

结语

如果你喜欢它,请给它 star :-)。如果你需要它,请添加 issues。如果你喜欢它,请 pull patches。如果你使用它,请写一篇博客 :-D。