Reputation: 9592
I want to loop through the columns of the model::find results. What I thought was possible is to cast the returning object to an array to be able to loop throught the columns, but that does not work.
Here is my Controller code:
<?php
class ManageController extends ControllerBase
{
public function indexAction()
{
$this->view->setVar("pages",(array) Pages::find());
}
}
And view code:
{% for key,value in pages %}
<p>key: {{key}}</p>
{% endfor%}
Any help would be usefull
Upvotes: 0
Views: 1467
Reputation: 13240
Use this;
<?php
class ManageController extends ControllerBase
{
public function indexAction()
{
$this->view->setVar("pages", Pages::find());
}
}
And view code:
{% for page in pages %}
{# in this case the key is just "0,1,2,3..." #}
{# so we use the loop index (or loop.index0 for zero based) #}
<p>This is the page #{{ loop.index }}</p>
<p>{{ page.title }}</p>
{% endfor%}
But if you really need to loop through the keys too, use:
{% for key, value in items %}
Key: {{ key }}
Value: {{ value }}
{% endfor%}
Upvotes: 2