1

smarty では、このような html コードに遭遇します。**

{section name=listAll loop=$scope} 
 (input id="a1" name="from" / >
 (input id="b1" name="from" / >
 (input id="c1" name="from" / >
{/section}

{section name=listAll loop=$scope} 
 (input id="a2" name="from" / >
 (input id="b2" name="from" / >
 (input id="c2" name="from" / >
{/section}

{section name=listAll loop=$scope} 
 (input id="a3" name="from" / >
 (input id="b3" name="from" / >
 (input id="c3" name="from" / >
{/section}

次のような関数に転送できますか:

        function RenderControl($i)
        {
        return '
        {section name=listAll loop=$scope} 
         (input id="a$i" name="from" / >
         (input id="b$i" name="from" / >
         (input id="c$i" name="from" / >
        {/section}
        } ';

次に、次のように tpl ファイルで呼び出します。

    {RenderControl i=1}
    {RenderControl i=2}
    {RenderControl i=3}

以下が smarty tpl で機能しないのはなぜですか?$smarty->register_function('RenderHtml','RenderHtml'); function RenderHtml($params){ extract($params); // $html= ' {include file="tke-pre_bid_scopeworkModules/Section1_Factory_to_Price_Optional_Configurat‌ ion.tpl"} ' ; $html を返します。}

{RenderHtml num=12}

4

2 に答える 2

2

テンプレート内から単純な再利用可能なテキスト ジェネレーターを定義できる{function}を探しているかもしれません。

{function name=controls i=0}
  (input id="a{$i}" name="from" / >
  (input id="b{$i}" name="from" / >
  (input id="c{$i}" name="from" / >
{/function}

{controls i=1}
{controls i=2}
{controls i=3}

入力の構造によっては、次のようなものが好きかもしれません

{function name=controls i=0}
  {$fields = ["a", "b", "c"]}
  {foreach $fields as $field}
    (input id="{$field}{$i}" name="from" / >
  {/foreach}
{/function}

これは Smarty3 の機能です。Smarty2 にはテンプレート機能がありませんでした。上記の {function} の内容を別のファイルに抽出することができます{include file="controls.tpl" i=1}。または、@Brett が言ったように、プラグイン関数を作成します。


あなたの質問の2番目の部分は、次のコードに関するものです

$smarty->register_function('RenderHtml','RenderHtml');

function RenderHtml($params){ 
  extract($params); 
  $html= '{include file="tke-pre_bid_scopeworkModules/Section1_Factory_to_Price_Optional_Configurat‌​ion.tpl"}'; 
  return $html;
}

あなたが期待しているように、これにはファイルが含まれません。これらのプラグイン関数が返すものは何でも、テンプレート出力に直接書き込まれます。あなたが何かをするのを止めるものは何もありません

function RenderHtml($params, &$smarty){ 
  // create new smarty instance
  $t = new Smarty();
  // copy all values to new instance
  $t->assign($smarty->get_template_vars());
  // overwrite whatever was given in params
  $t->assign($params);
  // execute the template, returning the generated html
  return $t->fetch('tke-pre_bid_scopeworkModules/Section1_Factory_to_Price_Optional_Configurat‌​ion.tpl');
}
于 2012-07-03T13:28:10.340 に答える
1

処理前にコードが評価されるようにするために、http://www.smarty.net/docs/en/advanced.features.prefilters.tplが必要な場合があるため、例のように実行できます。

http://www.smarty.net/docs/en/plugins.tplもご覧ください。

(上記は Smarty 3 の場合)

于 2012-07-03T12:21:41.643 に答える