Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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
Html 如果存在一个部分而不进行迭代,那么在Mustach中有没有一种方法可以渲染一个块?_Html_Mustache_Mustache.php - Fatal编程技术网

Html 如果存在一个部分而不进行迭代,那么在Mustach中有没有一种方法可以渲染一个块?

Html 如果存在一个部分而不进行迭代,那么在Mustach中有没有一种方法可以渲染一个块?,html,mustache,mustache.php,Html,Mustache,Mustache.php,我有一个带有标题的部分,我只想在填充数组时显示该部分,如下所示: <h1> These are the elements in the array </h1> <ul> {{#myArray}} <li>{{name}} - {{value}}</li> {{/myArray}} </ul> 这些是数组中的元素 {{{#myArray}} {{name}}-{{value} {{/myArray}

我有一个带有标题的部分,我只想在填充数组时显示该部分,如下所示:

<h1> These are the elements in the array </h1>
<ul>
    {{#myArray}}
    <li>{{name}} - {{value}}</li>
    {{/myArray}}
</ul>
这些是数组中的元素
    {{{#myArray}}
  • {{name}}-{{value}
  • {{/myArray}
仅当填充了
myArray
时,如何渲染

如果我把它放在#部分中,它会为数组中的每个元素渲染一次,这不是我想要的

正确的方法是什么?

{{{{myArray.length}
{{#myArray.length}}
     <h1> These are the elements in the array </h1>
{{/myArray.length}}
这些是数组中的元素 {{/myArray.length}
对于空数组,.length将返回0,因此我们获得了一个真正的false值

就PHP而言:

{{#myArray.0}}
     <h1> These are the elements in the array </h1>
{{/myArray.0}}
{{{#myArray.0}
这些是数组中的元素
{{/myArray.0}

这是正确的方法“正确”的方法是在渲染前准备视图,或者用方法传递对象:)它不起作用,可能是因为我使用的是php中的mustache,这意味着数组没有
.length
方法。更新了答案。在结束标记处有一个额外的#之前,请您尝试一下:{{{{{{{myArray.0}},我已经用
{{{{{myArray.0}}
更新了我的答案OK,谢谢。我会接受答案的。