2

I am trying to use Twig methods/loops while working on a design/template. To avoid touching the CP or the DB i did set variables directly in the template, like :

{% set service = 
  { 
    'titel': 
      [
        'Titel1', 
        'Titel2'
      ],
  }
%}

{% for i in 0..1 %}
  <p>{{ service.headerQuote }}</p>
{% endfor %}

It would be much better to be able to use the standard Craft methods, so if I move from templating/designing towards the DB i wouldn't have to touch the methods in my markup. Unfortunately, I can't make it work... I tried:

{% set service = { titel: 
                   [ 
                     'hallo',
                     'hallo2' 
                    ]
                  } 
                %}

{% for entry in craft.entries
      .section('service') %}

      <p>{{ entry.titel }}</p>

{% endfor %}

I tried to set the service entry to craft.entries.section('service') but that doesn't work as well... what am I missing?

Is it even possible to overwrite Craft variables in the markup?

2 Answers 2

2

Just use a variable that has the same keys as the entry model will have.

{% set entries = [{title: 'hello', ...}, {title: 'hello2', ...}] %}

{% for entry in entries %}
    {# stuff #}
{% endfor %}

Then, when you want to move to using the database, just switch what entries is set to:

{% set entries = craft.entries.section('service') %}

That way the template will still look exactly the same.

0
1

I'm not 100% sure I'm following what you're going for, if you want to access the array values of the titel property in the service variable, you'd do so like:

service.title[0], which would equal hallo and service.title[1], which would equal hallo2.

So would look like:

{% for entry in craft.entries.section(service.title[0]) %}

  <p>{{ entry.titel }}</p>

{% endfor %}
1
  • thanks a lot brad, i think my question was a bit confusing.
    – HendrikEng
    Commented Apr 25, 2017 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.