Waleed Ahmad
Waleed Ahmad

Reputation: 57

Pass on multiple arrays in October CMS

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>

I know this code is wrong but this is the idea. 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.

Upvotes: 0

Views: 204

Answers (1)

Hardik Satasiya
Hardik Satasiya

Reputation: 9715

You can write it like this

==
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>

this will work

if any doubts please comment.

Upvotes: 2

Related Questions