簡體   English   中英

我們可以在運行時編輯胡子模板嗎?

[英]Can we edit a mustache template in runtime?

我可以在運行時使用PHP在胡子模板中添加新節點嗎? 假設下面是一個代碼,其中ProductDetails將包含少量單個產品:

{{#ProductDetails}}
    {{#SingleProduct}} 
        {{OldDetail}}
    {{/SingleProduct}} 
{{/ProductDetails}}

我想添加一個新的節點像{{NewDetail}}剛過{{OldDetail}}通過在運行時的一些功能(即只是我編譯模板,這些模板已經發貨給客戶以這樣的方式在此之前,只有可以更改要編譯的代碼,但不能更改模板)? 我不想進行字符串操作(客戶至少創建了幾個帶有上述參數的新模板,但是間距可能會更改,並且他們可以在節點周圍添加一些新條目)。 小胡子庫是否為此提供任何功能?

如果我是你,我將嘗試Lambdas

Template.mustache將如下所示:

{{#ProductDetails}}
    {{#SingleProduct}} 
        {{OldDetail}}
    {{/SingleProduct}} 
{{/ProductDetails}}

代碼將類似於:

$Mustache = new Mustache_Engine(['loader' => new Mustache_Loader_FilesystemLoader($YOUR_TEMPLATE_FOLDER),]);
$Template   = $Mustache->loadTemplate('Template');

$OriginalOldDetail = '<h1>this is old detail</h1>';
echo $Template->Render([
    'ProductDetails'    => true, 
    'SingleProduct'     => true, 
    'OldDetail'         => function($text, Mustache_LambdaHelper $helper){
        // Render your original view first.
        $result = $helper->render($text);
        // Now you got your oldDetail, let's make your new detail.

        #do somthing and get $NewDetail;
        $NewDetail = $YourStuff;

        // If your NewDetail is some mustache format content and need to be render first.
        $result .= $help->render($NewDetail);

        // If is some content which not need to be render ? just apend on it.
        $result .= $NewDetail;

        return $result;
    }
]);

希望對您有所幫助。

(英語不是我的母語,因此希望您能理解我在說什么。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM