Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Suppress template not found error for modular pages

Solved by Ace A. View solution

Started by Ace A. 3 years ago · 3 replies · 579 views
3 years ago

I am currently building a theme compatibility behavior where a modular page, even those not supported by the theme, can dump the content of its modules into the page. I got the behavior I wanted, with a catch. When {{ module.content }} or {{ module.content|raw}} is used, in addition to the content of the page, the following error markup is being added:

HTML
<h3 style="color:red">ERROR: <code>modular/text.html.twig</code> template not found for page: <code>/demo/modular/_text</code></h3> <h1>_text</h1> 

I couldn't isolate these values with a dump in the debugbar, nor I can find a twig filter or function to do the trick. And for this scenario, renaming the modules to something theme-supported is not an option.

Is there anyway to force dump a module content without the errors popping up? Or for a way for me to access, restyle, or move the error in anyway? Thanks.

3 years ago

I found the culprit. The output is defined on `system/templates/modular/default.html.twig', which renders if the module template isn't found.

The next step is to figure out how to make sure that if your theme has a modular/default.html.twig, the theme will use that instead of the system fallback template.

3 years ago

Update: if you did define a modular/default.html.twig, the theme does use that in place of the system fallback. I just needed to clear all cache to see the changes.

However, when dumping the module object, the content is defined in #raw_content, not in content. module.raw_content only outputs the tags in the template, but not the raw content in the module object.

3 years ago Solution

Problem solved.

I've found that the best way to handle compatibility support for modular templates is by creating two files: a modular.html.twig and a modular/default.html.twig.

My modular.html.twig contains this basic collection loop:

TWIG
{% embed 'partials/base.html.twig' %}
{% block content %}
    {% for module in page.collection() %}
        {% include 'modular/default.html.twig' %}    
    {% endfor %} 
{% endblock content %}
{% endembed %}

There is a caveat though. I encountered a weird caching bug that does not let page.content()|raw work if page.content()|raw and module.content()|raw is not present in the template. After so many trial and errors, this is the only consistent workaround I found for my modular/default.html.twig

TWIG
<section>
    {% if module.content()|raw %}
        {{ module.content()|raw }}
    {% elseif page.content()|raw %}
        {{ page.content()|raw }}
    {% endif %}
</section>

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 197 2 months ago
Themes & Styling · by Ian, 2 months ago
3 93 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 127 3 months ago