我正在研究Twig Engine plugin for Wordpress。
这个想法是让设计师在创建WordPress主题的同时使用Twig。
不过,Wordpress基本上是建立在全局和全局函数之上的,而小枝约定是建立在OOP之上的。
我试图保持尽可能接近普通的旧php php wording的implimation。
Twig Recipe显示模板如何访问全局函数:
index.php文件:
$loader = new Twig_Loader_Filesystem(dirname(__FILE__));
$twig = new Twig_Environment($loader, array('cache' => false));
// ### ! begin Recipe from Twig Docs:
// auto-register all native PHP functions as Twig functions
// don't try this at home as it's not secure at all!
$twig->registerUndefinedFunctionCallback(function ($name) {
if (function_exists($name)) {
return new Twig_Function_Function($name);
}
return false;
});
// ### ! end recipe
$template = $twig->loadTemplate('index.html.twig');
$template->display(array('posts' => $posts));index.html.twig:
{# Twig flavored The Loop #}
{% if have_posts() %}
{# This is a hacked wordpress loop - its modified to work with a for loop #}
{% for post in posts %}
{{ the_post() }}
{{ the_title() }}
{{ the_content() }}
{% endfor %}
{% endif %}我的问题:是否允许树枝模板访问任何原生PHP函数比普通的旧PHP安全性要差得多?