pkj/minibase-plugin-twig

此插件用 Twig 替换了 PHP 模板引擎。非常适合喜欢 Twig 的开发者。

dev-master 2013-05-05 02:08 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:18 UTC


README

启用基于 Twig 的模板。使用 Minibase 提供的相同接口来渲染常规 PHP 视图。还包括自定义块、过滤器和函数,以充分利用 Minibase。轻松实现 片段缓存

此插件使您能够使用 Twig 模板引擎渲染 .twig.html 文件。.php 文件仍然由默认的 Minibase 视图渲染引擎处理。

安装

{
  "require":{
	     "pkj/minibase-plugin-twig": "dev-master"
	}
}

设置

设置全局视图路径,我们需要知道 Twig 模板所在的位置。

$mb->setConfig(MB::CFG_VIEWPATH, __DIR__ . '/views');

初始化插件

$mb->initPlugins(array(
	'Pkj\Minibase\Plugin\TwigPlugin\TwigPlugin' => array(
		// Where to store the compiled php templates.		
		'cache' => __DIR__ . '/template_compilation_cache'
	)
));

开始使用 Twig 模板。

函数

route

反向路由简单。

Reverse routing:


{{"News.show"|route(32)}} 
Prints forexample: /news/32

{{"News.index"|route()}}
Prints forexample: /news
Check if a route is active:

{% if "News.index"|route().isActive %}
Currently on homepage.
{% endif %}

过滤器

asset

轻松添加资产,资产将包含正确的基路径。

<link rel="stylesheet" type="text/css" href="{{"css/style.css"|asset}}" />

Or from a variable
<img src="{{news.img_path|asset}}" />

标签

cache

轻松缓存视图中的部分,如果你使用具有懒加载功能的 ORM(如 Doctrine)则非常好。

{% cache "uniqueKey1" %}
	
	Hello I am cached forever!
	
{% endcache %}


{% cache "uniqueKey2" 3600 %}

	I am cached for 1 hour.

{% endcache %}