我正在加载一个twig模板,它包含一个try:
result = tineye.search_url(url)
except NameError:
print 'Nothing Found !'
break
if result.total_results:
results.extend(result.matches)
循环,运行一个类型数组的变量并输出ok,现在我试图在for循环中包含一段html并传入来自循环的数据并填充子模板。
mainSet.template.php
for
{% for set in sets %}
{% include 'inner/set.template.php' with {'set'} only %}
{% endfor %}
,inner是inner/set.template.php
所在目录中的子文件夹。
set.template.php
mainSet.template.php
这是我得到的错误:
<div class="set">
<span>{{ set.name }}</span>
</div>
代码是分解在一个自定义框架上,在这里发布所有内容,初始twig模板加载如下,并传入一组设置数据。
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}" ("punctuation" expected with value ":") in "index" at line 4.' in C:\htdocs\vendor\twig\twig\lib\Twig\TokenStream.php:87
Stack trace:
#0 C:\htdocs\vendor\twig\twig\lib\Twig\ExpressionParser.php(284): Twig_TokenStream->expect(9, ':', 'A hash key must...')
#1 C:\htdocs\vendor\twig\twig\lib\Twig\ExpressionParser.php(188): Twig_ExpressionParser->parseHashExpression()
#2 C:\htdocs\vendor\twig\twig\lib\Twig\ExpressionParser.php(84): Twig_ExpressionParser->parsePrimaryExpression()
#3 C:\htdocs\vendor\twig\twig\lib\Twig\ExpressionParser.php(41): Twig_ExpressionParser->getPrimary()
#4 C:\htdocs\vendor\twig\twig\lib\Twig\TokenParser\Include.php(46): Twig_ExpressionParser->parseExpression()
#5 C:\htdocs\ in C:\htdocs\vendor\twig\twig\lib\Twig\TokenStream.php on line 87
$ indexData
$twigLoader = new Twig_Loader_Array(array(
'index' => file_get_contents((!empty($dirLevel) ? $dirLevel : '').TEMPLATE_DIR . 'index' . TEMPLATE_EXT)
));
...
$twig = new Twig_Environment($twigLoader);
$page = $twig->render('index', $indexData);
甚至可以做我正在尝试的事情,我知道你可以在模板中包含模板,所以也许我有一些语法错误!?
答案 0 :(得分:2)
将{% include 'inner/set.template.php' with {'set'} only %}
更改为{% include 'inner/set.template.php' with {'set':set} only %}
关联数组的语法是{key1:value1, key2:value2, ...}