是否允许twig模板访问任何本机PHP函数,而不是普通的旧PHP?

时间:2012-03-14 12:36:03

标签: wordpress twig

我在Twig Engine plugin for Wordpress工作。 这个想法是允许设计师在创建wordpress主题时使用Twig。

然而,Wordpress主要建立在全局和全局函数上,而Twig约定建立在OOP之上。 我试图尽可能接近普通的旧的php wordpress主题。

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() }}
            <article id="post-{{ the_id() }}">
                <h1>{{ the_title() }}</h1>
                <section class="entry-content">
                    {{ the_content() }}   
                </section>
            </article>
        {% endfor %}
{% endif %}

我的问题:允许twig模板访问任何原生PHP函数的安全性明显低于普通的旧PHP吗?

1 个答案:

答案 0 :(得分:1)

没有。 因为你基本上只将twig的所有未知函数映射到(普通旧的)PHP,所以你不能在PHP中做更多的事情。

现在没有什么可以做的,这在纯PHP模板中也是不可能的。