This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

walle92
walle92

I have a list in October CMS like this:

<ul>
   <li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
      <a href="{{'dir/page' | page}}">Item 1</a>
   </li>
   <li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
      <a href="{{'dir/page' | page}}">Item 2</a>
   </li>
   <li {% if this.page.id=='some-id' %} class = "active" {% endif %}>
      <a href="{{'dir/page' | page}}">Item 3</a>
   </li>
</ul>

However, as you can see there is a lot of repitition so I decided to create array variables. I can pass on a single array variable but I am not sure how to do it for multiple arrays so my code can look something like this:

==
function onStart(){
    $this['pageData']=[
    'data' => [
        'item' => ['Item 1','Item 2','Item 3'],
        'id' => ['pageid1','pageid2','pageid3'],
        'link' => ['dir/page1','dir/page2','dir/page3']
    ]
    ];
}
==
<ul>
   {% for items in pageData %}
   <li {% if this.page.id == items.data.id %} class="active" {% endif %}>
     <a href="{{items.data.link | Page}}">{{items.data.item}}</a>
   </li>
   {% endfor %}
</ul>

It's just an example. I am not really using the php inside partial. I can retrieve one object array if I write:

{% for items in pageData.data.item %}
  <li>{{items}}</li>
{% endfor %}

But this of course only gives me a list of items inside the 'item' object. I hope I explained it well. Please let me know if I can provide more information to get this problem solved

walle92
walle92

Hey guys. I found solution to my problem. I was using incorrect array syntax. This is how it worked for me:

==
function onStart(){
    $this['records'] = [    
      [
        'id' =>  'pageid1',
        'item' => 'Item 1',
        'link' => 'dir/page1'
      ],
      [
        'id' =>  'pageid2',
        'item' => 'Item 2',
        'link' => 'dir/page2'
      ],
      [
        'id' =>  'pageid3',
        'item' => 'Item 3',
        'link' => 'dir/page3'
      ],
    ];
}
====
<ul>
    {% for record in records %}
       <li {% if this.page.id == record.id %} class="active" {% endif %}>
         <a href="{{record.link | page}}">{{record.item}}</a>
       </li>
    {% endfor %}
</ul>

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.