<p>i have a big problem on my site. One for loop requires more than 30 seconds. When i put the loop in a comment, the page loads shortly. So the loop must be the problem.</p>

<p>Is there any trick to accelerate a loop?</p>

<p>Here is the loop code: </p>

<pre><code>{% for program in craft.entries.section('program_point').order('program_number') %}
    {% if program.block.first().title == program_block.title %}
        &lt;div class="seminar-box"&gt;
            {% if program.type == "seminar" %}&lt;a href="{{ program.url }}"&gt;{% endif %}
            &lt;h4&gt;&lt;span&gt;{% if program.type != "seminar" %}#{% endif %}{{ program.program_number }}&lt;/span&gt;{{ program.title }}&lt;/h4&gt;
            {% if program.short_description|length %}
                &lt;p&gt;{{ program.short_description }}&lt;/p&gt;
            {% endif %}
            {% if program.type == "seminar" %}&lt;/a&gt;{% endif %}
            {% if program.program_referents|length %}
                &lt;span&gt;
                    {% set i = 1 %}
                    {% for referent in program.program_referents %}
                        &lt;!-- &lt;a href="{{ referent.url }}" class="referent-link"&gt;--&gt; {{ referent.referent_name }}&lt;!--&lt;/a&gt;--&gt; {% if i != program.program_referents|length %}, {% endif %}
                        {% set i = i+1 %}
                    {% endfor %}
                &lt;/span&gt;
            {% endif %}
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-08-13T21:10:12.237Z
Could you post the loop code? As you'd say it's likely something in there, but it will be pretty hard to comment on without the code ;)
Josh Angell
CC BY-SA 3.0
2015-08-13T21:19:45.330Z
Great thanks - could you try commenting out the inner for loop? The one looping over `program.program_referents`.
Josh Angell
CC BY-SA 3.0
2015-08-13T21:49:23.010Z
this requires nearly the some time
Jonathan Schiffner
CC BY-SA 3.0
2015-08-14T02:52:43.570Z
Not sure if this is the cause of the speed issue, but you can remove the variable ```i``` and use twig's built in ```loop.index``` and ```loop.last```

http://twig.sensiolabs.org/doc/tags/for.html
j00lz
CC BY-SA 3.0
2015-08-14T02:54:55.833Z
Also try wrapping the inner loop in cache tags: ```{% cache %}<!-- your loop here -->{% endcache %}
j00lz
CC BY-SA 3.0
2015-08-14T05:35:30.600Z
In which case it must be the outer loop - perhaps try adding a limit? However, I would try removing all the code inside the loop first to see if any of that is causing the issue and work outwards from there.
Josh Angell
CC BY-SA 3.0
2015-08-14T07:13:08.427Z
now i have comment out all inside the loop and it takes 7 sec to load. 
with the first if query it takes nearly 30 sec. So the first if might be the problem. Any idea why?
Jonathan Schiffner
CC BY-SA 3.0
2015-08-14T07:24:41.360Z
@j00lz {%cache%} is a great idea, but then the if-query '{% if program.type == "seminar" %}' does not work. An idea why?
Jonathan Schiffner
CC BY-SA 3.0
2015-08-14T07:41:04.287Z
are you sure? cache should not effect that at all...
j00lz
CC BY-SA 3.0
2015-08-14T08:40:47.907Z
@j00lz yes i'm realy sure
Jonathan Schiffner
CC BY-SA 3.0
2015-08-14T09:48:22.893Z
@JonathanSchiffner right - by commenting out the inside of the loop you are determining that it is definitely something inside there, so add things back in line by line until you hit the part that adds the extra 20s. Then you have found the actual problem and can work on solving that. For example: comment out everything inside that first `{% if %}` statement and see what difference that makes.
Josh Angell
CC BY-SA 3.0
2015-08-14T12:24:46.780Z
is it possible to load first the html content and than the craft loop?
Jonathan Schiffner
CC BY-SA 3.0
2015-08-14T14:18:17.147Z
I'm not sure what you mean.
Josh Angell
CC BY-SA 3.0
2015-08-30T17:02:58.040Z
a new try:
i only use this loop: `{% for program in craft.entries.section('program_point').order('program_number') %}` and it takes 25s . The loop returns 81 data. Any idea how to fix this problem?
Jonathan Schiffner
<templating><performance>
TTBF about 30 seconds
# TTBF about 30 seconds

(Asked by: username_0 on 2015-08-13)

i have a big problem on my site. One for loop requires more than 30 seconds. When i put the loop in a comment, the page loads shortly. So the loop must be the problem.
Is there any trick to accelerate a loop?
Here is the loop code: 
{% for program in craft.entries.section('program_point').order('program_number') %}
    {% if program.block.first().title == program_block.title %}
        
            {% if program.type == "seminar" %}{% endif %}
            {% if program.type != "seminar" %}#{% endif %}{{ program.program_number }}{{ program.title }}
            {% if program.short_description|length %}
                {{ program.short_description }}
            {% endif %}
            {% if program.type == "seminar" %}{% endif %}
            {% if program.program_referents|length %}
                
                    {% set i = 1 %}
                    {% for referent in program.program_referents %}
                         {{ referent.referent_name }} {% if i != program.program_referents|length %}, {% endif %}
                        {% set i = i+1 %}
                    {% endfor %}
                
            {% endif %}
        
    {% endif %}
{% endfor %}

**Tags:** templating, performance
> **Comments:**
> **username_1**: Could you post the loop code? As you'd say it's likely something in there, but it will be pretty hard to comment on without the code ;)  
> **username_1**: Great thanks - could you try commenting out the inner for loop? The one looping over `program.program_referents`.  
> **username_0**: this requires nearly the some time  
> **username_2**: Not sure if this is the cause of the speed issue, but you can remove the variable ```i``` and use twig's built in ```loop.index``` and ```loop.last```  http://twig.sensiolabs.org/doc/tags/for.html  
> **username_2**: Also try wrapping the inner loop in cache tags: ```{% cache %}<!-- your loop here -->{% endcache %}  
> **username_1**: In which case it must be the outer loop - perhaps try adding a limit? However, I would try removing all the code inside the loop first to see if any of that is causing the issue and work outwards from there.  
> **username_0**: now i have comment out all inside the loop and it takes 7 sec to load.  with the first if query it takes nearly 30 sec. So the first if might be the problem. Any idea why?  
> **username_0**: @j00lz {%cache%} is a great idea, but then the if-query '{% if program.type == "seminar" %}' does not work. An idea why?  
> **username_2**: are you sure? cache should not effect that at all...  
> **username_0**: @j00lz yes i'm realy sure  
> **username_1**: @JonathanSchiffner right - by commenting out the inside of the loop you are determining that it is definitely something inside there, so add things back in line by line until you hit the part that adds the extra 20s. Then you have found the actual problem and can work on solving that. For example: comment out everything inside that first `{% if %}` statement and see what difference that makes.  
> **username_0**: is it possible to load first the html content and than the craft loop?  
> **username_1**: I'm not sure what you mean.  
> **username_0**: a new try: i only use this loop: `{% for program in craft.entries.section('program_point').order('program_number') %}` and it takes 25s . The loop returns 81 data. Any idea how to fix this problem?  

---

## 0 answers

