Calling loop on Partials is deprecated?

Hello everyone,

I am getting the following warning in the Depricated section of the Grav Debugger:

array:4 [
  "message" => "Calling "loop" on template "partials/randomimage.html.twig" from template "partials/randomimage.html.twig" is deprecated since version 1.28 and won't be supported anymore in 2.0."
  "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/partials/randomimage.html.twig"
  "line" => 16
  "trace" => array:5 [
    0 => array:2 [
      "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/partials/randomimage.html.twig"
      "line" => 16
    ]
    1 => array:2 [
      "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/partials/randomimage.html.twig"
      "line" => 13
    ]
    2 => array:2 [
      "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/partials/base.html.twig"
      "line" => 56
    ]
    3 => array:2 [
      "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/partials/base.html.twig"
      "line" => 1
    ]
    4 => array:2 [
      "file" => "/Applications/MAMP/htdocs/grav-admin/user/themes/pauls-theme/templates/index.html.twig"
      "line" => 1
    ]
  ]
]

Does anyone have an idea what I am doing wrong? and how to resolve this?
This is the code I am using.

{% macro loop(page) %}
    <div class="random-preview">
        {# go through two random pages that are visible #}
        {% for p in page.children.visible.random(1) %}
            {% if p.header.thumbnail|length > 1 %}
                {{ p.media[p.header.thumbnail].html("titel", "alt", "random-image padding") }}

                <!-- {% set pic = p.media.images %}
                {% set randompic = random(pic) %}
                {{ randompic.html("titel", "alt", "random-image padding") }} -->
            {% endif %}
        {% endfor %}
    </div>    
{% endmacro %}

{{ _self.loop(pages) }}

Thanks in advance,
EE

@eduardeisman, Since Twig 2.0 the definition and the calling of a macro can no longer be in the same template.

Have a look at how Quark uses macros in ‘templates/partials/navigation.html.twig’ and ‘templates/macros/macro.html.twig’.

In short:

  • Create file '/templates/macros/loop.html.twig and define the macro in that file.
  • In the template where you want to call the macro, import the macro and call it as follows:
    {% import 'macros/loop.html.twig' as macros %}
    ...                                    
    {{ macros.loop(pages) }}           
    
1 Like

Ahh okay! Cool Thank You! :slight_smile: