Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 胡须中的复杂模板合成_Templates_Template Engine_Mustache - Fatal编程技术网

Templates 胡须中的复杂模板合成

Templates 胡须中的复杂模板合成,templates,template-engine,mustache,Templates,Template Engine,Mustache,考虑一个显示用户冰箱内容的web应用程序。除此之外,还需要显示冰箱中当前存放的水果列表。每种水果都需要非常不同的展示。最重要的是,将有大量不同的水果类型 我想使用一个Mustache模板库来实现这一点,但我不太确定最好的方法是什么 首先,以下是每种水果的模板: {{! apple.mustache }} A {{appleType}} apple from {{groceryStore}} {{! orange.mustache }} An orange from {{countryOfOri

考虑一个显示用户冰箱内容的web应用程序。除此之外,还需要显示冰箱中当前存放的水果列表。每种水果都需要非常不同的展示。最重要的是,将有大量不同的水果类型

我想使用一个Mustache模板库来实现这一点,但我不太确定最好的方法是什么

首先,以下是每种水果的模板:

{{! apple.mustache }}
A {{appleType}} apple from {{groceryStore}}

{{! orange.mustache }}
An orange from {{countryOfOrigin}}

{{! banana.mustache }}
A {{ripeness}} banana

...many more templates here...
方法1

让“视图模型”或“视图助手”对象通过创建将传递给模板的“isBanana”/“isOrange”/etc.键来准备模板数据。在本例中,冰箱模板将如下所示:

You have the following food in your fridge:

{{#fruits}}
        {{#isApple}}
                {{> apple}}
        {{/isApple}}
        {{#isOrange}}
                {{> orange}}
        {{/isOrange}}
        {{#isBanana}}
                {{> banana}}
        {{/isBanana}}
        ...more conditionals....
{{/fruits}}
{{# items }}
  {{# getPartial }}{{ type }}{{/ getPartial }}
{{/ items }}
我在互联网上看到过一些推荐这种方法的地方。然而,我看不出它将如何扩展:每次添加新的水果类型时,冰箱模板都必须修改。这似乎也与小胡子的“无逻辑”哲学背道而驰

方法2

让视图模型负责为每种水果类型确定正确的模板,呈现它,并将HTML作为模板的数据返回。冰箱模板现在看起来像:

You have the following food in your fridge:

{{{fruits}}}
以及视图模型:

class InventoryViewModel
{
    // ...

    function getFruits()
    {
        foreach ($this->fridge->getFruits() as $fruit) {
            if ($fruit instanceof Banana) {
                $html .= $this->mustache->render(...);
            } elseif ($fruit instanceof Apple) {
                $html .= $this->mustache->render(...);
            } else {
                // ...
            }
        }

        return $html;
    }
}
该选项似乎比第一个选项更好,但它需要向每个视图模型中注入一个胡须模板渲染对象。如果可能的话,我希望避免这种耦合

方法3

使用某种模板合成功能,这不是官方胡须规范的一部分。(等)


这些选项中哪一个是最好的,为什么?我是否忽略了更好的内容?

最简单的方法可能是在ViewModel中使用高阶部分(lambda部分)为您选择部分

基本模板的外观如下所示:

You have the following food in your fridge:

{{#fruits}}
        {{#isApple}}
                {{> apple}}
        {{/isApple}}
        {{#isOrange}}
                {{> orange}}
        {{/isOrange}}
        {{#isBanana}}
                {{> banana}}
        {{/isBanana}}
        ...more conditionals....
{{/fruits}}
{{# items }}
  {{# getPartial }}{{ type }}{{/ getPartial }}
{{/ items }}
当然,假设你的水果有“类型”。然后为
getPartial
添加一个高阶部分:

<?php

class InventoryViewModel
{
    // ...

    function getPartial()
    {
        // you could do string manipulation here,
        // but I prefer the explicitness of a `case`

        return function($tpl, $mustache) {
            switch ($mustache->render($tpl)) {
                case 'Banana':
                    return '{{> banana }}';
                case 'Apple':
                    return '{{> apple }}';
                case 'Orange':
                    return '{{> orange }}';
            }
        };
    }
}

最简单的方法可能是在ViewModel中使用高阶部分(lambda部分)为您选择部分

基本模板的外观如下所示:

You have the following food in your fridge:

{{#fruits}}
        {{#isApple}}
                {{> apple}}
        {{/isApple}}
        {{#isOrange}}
                {{> orange}}
        {{/isOrange}}
        {{#isBanana}}
                {{> banana}}
        {{/isBanana}}
        ...more conditionals....
{{/fruits}}
{{# items }}
  {{# getPartial }}{{ type }}{{/ getPartial }}
{{/ items }}
当然,假设你的水果有“类型”。然后为
getPartial
添加一个高阶部分:

<?php

class InventoryViewModel
{
    // ...

    function getPartial()
    {
        // you could do string manipulation here,
        // but I prefer the explicitness of a `case`

        return function($tpl, $mustache) {
            switch ($mustache->render($tpl)) {
                case 'Banana':
                    return '{{> banana }}';
                case 'Apple':
                    return '{{> apple }}';
                case 'Orange':
                    return '{{> orange }}';
            }
        };
    }
}