Traxstar Traxstar - 4 months ago 26
PHP Question

Symfony Pass Data to view

My Question is, how I can send Data to my TWIG View in the Symfony Framework.
I guess I will need an action that is passing the data towards it.

So I startet creating an action that connects to the Database and adds the data I need to an array that looks like :

$data[$name] = $name;


At the end i render this:

return $this->render('PoolLinkmotorBundle:Page:index.html.twig', array('motos' => $data));


What do i need to do in my View now?
I thought i can do somethink like

{% for data in motos %}
{{ //Dont know what to do here }}
{% endfor %}


Any ideas ?

UPDATE:

This is the view where i need to add the Data. It is in file called '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 %}

The Data belongs to the part where 'id == 15';

UPDATE:

I figured it out by myself.
The Data i am passing just just can be accesed in the view by init an array and put the data in . The rendering was right.

Answer

For the Loop, you can check : http://twig.sensiolabs.org/doc/tags/for.html

But motos should be an array, not only a string name

 $arrayExample = [ [0] => ['name' => 'Yamaha'], [1] => ['name' => 'Honda'] ]; // motos in twig

{% for data in motos %} 
{{ data.name }} // will display Yamaha // Honda 
{% endfor %}

If it's only a string :

$data['name'] = 'toto';

{{ myvartwig.name }} // NO LOOP