2012-03-14 2 views
0

私はTwig Engine plugin for Wordpressに取り組んでいます。 デザイナーがWordpressテーマを作成しながらTwigを使用できるようにすることです。twigテンプレートにネイティブのPHP関数へのアクセスが、普通の古いPHPよりも安全性が低くなっていますか?

しかし、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 %} 

私の質問を :枝を許しているネイティブPHP関数へのテンプレートアクセスは、普通の古いPHPよりはるかに安全ですか?

+0

TwigをベースシステムとしたTimber(Wordpress Plugin)でもこれを行う方法。それは常に例外をスローする - "Uncaught例外 'Twig_Error_Syntax'メッセージ '関数"が爆発 "存在しない..." – waplet

+0

@waplet https://github.com/jarednova/timberで著者に尋ねてみましたか? – max

答えて

1

番号 基本的には、twigのすべての未知関数を(普通の古い)PHPにマッピングするだけなので、PHPで行うことはできません。

純粋なPHPテンプレートではこれはできません。

関連する問題