我是一个新的 Twig 用户,我在填充网格时遇到了麻烦......我有一个名为“菜单”的实体,并且只想访问一些属性,所以我创建了一个这样的数组:
$itens = array(
array('name' => 'id', 'label' => 'Id'),
array('name' => 'parent', 'label' => 'Pai'),
array('name' => 'name', 'label' => 'Nome'),
array('name' => 'route', 'label' => 'Rota'),
array('name' => 'position', 'label' => 'Posição'),
);
Run Code Online (Sandbox Code Playgroud)
所以我试图做一个嵌套循环来访问这些信息......但我不知道怎么做。我这样试过,但显然这是错误的。
<tbody>
{% for menu in menus %}
<tr>
{% for item in itens %}
<td> {{ menu.[item.name] }} </td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Run Code Online (Sandbox Code Playgroud)
我该怎么做???
在 PHP 代码中,它看起来像这样:
foreach $menus as $menu {
echo "<tr>";
foreach $itens as $item …Run Code Online (Sandbox Code Playgroud) 我正在尝试建立这样的连接:
{% set img_src='images/ksiazki/'~{{new_book.tytul}}~'.jpg' %}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到此错误:
哈希键必须是带引号的字符串、数字、名称或括在括号中的表达式(值“的意外标记“标点符号”)
怎么了?
我的树枝模板中有以下代码来操作数据库:
{%
$tablename = $app['config']->get('general/database/prefix') . 'entries';
$query = "UPDATE $tablename SET $views = ? WHERE id = ?";
$views = record.views + 1;
$stmt = $app['db']->prepare($query);
$stmt->bindValue(1, $views);
$stmt->bindValue(2, record.id);
$res = $stmt->execute();
%}
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中加载模板时,出现以下错误:
Twig_Error_Syntax
Unexpected character "$" in "record.twig" at line 18.
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?
对于我使用的 symfony 形式和树枝
{{ form_errors(form) }}
Run Code Online (Sandbox Code Playgroud)
使用bootstrap_4_horizontal_layout.html.twig,它工作正常,输出如下:
<span class="alert alert-danger d-block"><span class="d-block">
<span class="form-error-icon badge badge-danger text-uppercase">Error</span> <span class="form-error-message">Error</span>
</span></span>
Run Code Online (Sandbox Code Playgroud)
但我需要对其进行自定义以翻译消息旁边的引导错误标签。我可以在哪里定制它?
在我的 Symofny 项目中,我正在为具有特定状态的实体显示按钮。
如果我只为一种状态定义它可以工作,但是当我尝试使用两种状态时它不会。但它适用于如果我把例如' !='。
{% if (item.status == 'free') and (item.status == 'paid') %}
<a type="button" href="{{ path('my_path', {'id':item.id}) }}" class="btn btn-primary">Btn</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
也same as按照树枝文档中的描述进行了尝试。
我正在研究一个Symfony2项目,我想调用一个JavaScript函数并从Twig传递一组JSON对象(我从控制器获得).
但是第一个非常简单的测试已经失败,例如:
main.js:
function helloWorld(name) {
console.log("hello " + name);
}
Run Code Online (Sandbox Code Playgroud)
链接到main.js 树枝和调用的函数:
<body>
<script>helloWorld("world!")</script>
{% block javascripts %}
<script src="{{ asset('js/main.js') }}"></script>
{% endblock %}
</body>
Run Code Online (Sandbox Code Playgroud)
这会导致ReferenceError:
"未捕获的ReferenceError:未定义helloWorld"
为了使这项工作,我必须做些什么?
编辑:感谢两位花时间回答的人.描述的枝条实际上由一堆嵌套的树枝组成,javascript包含的位置是基于Symfony文档,猜测这就是为什么我没有看到明显的.虽然在表达问题时应该自己检测到问题....
我必须将一个动作的模板呈现为一个简单的.txt文件。
我怎样才能做到这一点?除了使用Response对象还有其他方法吗?
使用Response对象:
$content = $this->get('templating')->render(
'AppBundle:Company:accountBillingInvoice.txt.twig',
[
'invoice' => 'This is the invoice'
]
);
$response = new Response($content , 200);
$response->headers->set('Content-Type', 'text/plain');
Run Code Online (Sandbox Code Playgroud) 为大家快速提问,
是否有什么我遗漏的地方,我无法找到正确的答案,或者我正在阅读我发现错误的结果。
Bascially, I have a variable in php
ex $var="<b>#Something#</b>";
And i render it via TWIG like
{{ var }}
Run Code Online (Sandbox Code Playgroud)
我不希望它实际呈现<b>#Something#</b>,但我想要#Something#
我在简单的任务中失败的任何想法?
我想知道这个符号是什么?意思是,当我们使用它
{{ entity.date|date('d-m-Y')}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解释一下吗?