我的问题是,如何将数据发送到Symfony框架中的TWIG View。 我想我需要一个将数据传递给它的动作。
所以我开始创建一个连接到数据库的动作,并将我需要的数据添加到一个看起来像这样的数组:
$data[$name] = $name;
最后我渲染了这个:
return $this->render('PoolLinkmotorBundle:Page:index.html.twig', array('motos' => $data));
我现在在视图中需要做什么? 我以为我可以做一些想法
{% for data in motos %}
{{ //Dont know what to do here }}
{% endfor %}
有什么想法吗?
更新
这是我需要添加数据的视图。它位于名为' macros.twig.html'的文件中。
{% macro showPagesBodyCell(tableOption, page, type, previousPageId) %}
{% set id = tableOption.id %}
{% set class = 'row-' ~ id ~ ' ' ~ tableOption.class %}
{% if id == 1 %}
<td class="url {{ class }}">
<a id="prospect-{{ page.id}}" href="{{ path('pool_linkmotor_pages_view', {id:page.id}) }}" title="{{ page.full }}">
{{ page.fullForDisplay }}
</a>
</td>
{% elseif id == 2 %}
<td class="lm-actions {{ class }}">
<div class="uk-button-group">
<a title="{% trans %}Open{% endtrans %}" class="uk-button lmexternal" href="{{ path('pool_linkmotor_anonymizer') ~ '?url=' ~ page.full }}" target="_blank">
<i class="uk-icon-external-link"></i>
</a>
<a title="{% trans %}Search for backlinks{% endtrans %}" class="uk-button slow" href="{{ path('pool_linkmotor_pages_search_backlinks', {id:page.id}) }}">
<i class="uk-icon-link"></i>
<i class="uk-icon-plus icon-backlink"></i>
</a>
</div>
</td>
{% elseif id == 3 %}
<td class="{{ class }}">
{% if page.statusMayBeChangedByUser(app.user) %}
<form class="autosubmit" action="{{ path('pool_linkmotor_pages_edit', {id:page.id}) }}" method="POST">
<input type="hidden" name="inline" value="true">
<input type="hidden" name="previous" value="{{ previousPageId }}">
<select name="page_edit[status]">
<option value="1"{% if page.status.id == 1 %} selected="selected"{% endif %}>
{% trans %}New{% endtrans %}
</option>
<option value="2"{% if page.status.id == 2 %} selected="selected"{% endif %}>
{% trans %}Relevant{% endtrans %}
</option>
<option value="3"{% if page.status.id == 3 %} selected="selected"{% endif %}>
{% trans %}Not relevant{% endtrans %}
</option>
<option value="4"{% if page.status.id == 4 %} selected="selected"{% endif %}>
{% trans %}1. Contact{% endtrans %}
</option>
<option value="5"{% if page.status.id == 5 %} selected="selected"{% endif %}>
{% trans %}2. Contact{% endtrans %}
</option>
<option value="8"{% if page.status.id == 8 %} selected="selected"{% endif %}>
{% trans %}In progress{% endtrans %}
</option>
</select>
</form>
{% else %}
{{ page.status.name|trans }}
{% endif %}
</td>
{% elseif id == 4 %}
<td class="{{ class }}">{{ valueOrSpinner(page.authority, 0) }}</td>
{% elseif id == 5 %}
<td class="{{ class }}">{{ valueOrSpinner(page.subdomain.domain.authority, 0) }}</td>
{% elseif id == 6 %}
<td class="{{ class }}">
{% if not page.subdomain.domain.notYetCrawled and not page.subdomain.domain.firstYear %}
{% trans %}n/a{% endtrans %}
{% else %}
{{ valueOrSpinner(page.subdomain.domain.firstYear) }}
{% endif %}
</td>
{% elseif id == 7 %}
<td class="{{ class }}">{{ valueOrSpinner(page.subdomain.domain.netPop|localeNumber, null) }}</td>
{% elseif id == 8 %}
<td class="{{ class }}">{{ valueOrSpinnerOrInfo('sistrix', page.subdomain.sichtbarkeitsindex|localeNumber(4), null) }}</td>
{% elseif id == 9 %}
<td class="{{ class }}">{{ valueOrSpinnerOrInfo('xovi', page.subdomain.ovi|localeNumber(2), null) }}</td>
{% elseif id == 10 %}
<td class="{{ class }}">
<a href="{{ path('pool_linkmotor_domains_view', {id:page.subdomain.domain.id}) }}" class="domain">
{{ page.subdomain.domain.nameForDisplay }}
</a>
</td>
{% elseif id == 11 %}
<td class="{{ class }}" style="min-width: 75px;">
{% if page.subdomain.vendor %}
<a class="uk-text-muted" href="{{ path('pool_linkmotor_vendors_view', {id:page.subdomain.vendor.id}) }}"
data-uk-tooltip="{pos:'bottom'}" title="Subdomain-{% trans %}Vendor{% endtrans %}: {{ page.subdomain.vendor.displayName }}">
<i class="uk-icon-user"></i> <small>{% trans %}show{% endtrans %}</small>
</a>
{% elseif page.subdomain.domain.vendor %}
<a data-uk-tooltip title="{{ page.subdomain.domain.vendor.displayName }} {{ page.subdomain.domain.vendor.phone }}" href="{{ path('pool_linkmotor_vendors_view', {id:page.subdomain.domain.vendor.id}) }}">
<i class="uk-icon-user"></i> <small>{% trans %}show{% endtrans %}</small>
</a>
{% else %}
<a class="uk-text-muted" href="{{ path('pool_linkmotor_domains_set_vendor', {id:page.subdomain.domain.id}) }}">
<i class="uk-icon-plus-square"></i> <small>{% trans %}add{% endtrans %}</small>
</a>
{% endif %}
</td>
{% elseif id == 12 and type == 'all'%}
<td class="{{ class }}">{{ page.assignedTo.displayName }}</td>
{% elseif id == 13 %}
<td class="{{ class }}">
{% if page.lastNote %}
<span data-uk-tooltip="{pos:'bottom'}" title="{{ page.lastNote.content }}">
<i class="uk-icon-comment"></i>
</span>
{% endif %}
</td>
{% elseif id == 14 %}
<td class="{{ class }}"><small>{{ page.lastModifiedAt|localeDate('d') }}</small></td>
{% elseif id == 15 %}
{% endif %}
{%endmacro%}
数据属于&#39; id == 15&#39;;
的部分更新:
我自己想通了。 刚刚传递的数据可以通过init数组在视图中加入并将数据放入。渲染是对的。
答案 0 :(得分:1)
对于循环,您可以查看:http://twig.sensiolabs.org/doc/tags/for.html
但是motos
应该是一个数组,而不仅仅是一个字符串名称
$arrayExample = [ [0] => ['name' => 'Yamaha'], [1] => ['name' => 'Honda'] ]; // motos in twig
{% for data in motos %}
{{ data.name }} // will display Yamaha // Honda
{% endfor %}
如果它只是一个字符串:
$data['name'] = 'toto';
{{ myvartwig.name }} // NO LOOP