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

How to render content in different blocks

Started by Muut Archive 10 years ago · 7 replies · 637 views
10 years ago

I understand that everything inside the pages folder is being rendered in the content block.
But I also have different types of content that I would like to render in another block.

For instance: I am using the foundation framework and generally all content is rendered within a div with a row class which defines the pages boundaries. But on my homepage I want to place an image slider which uses the full browser's width.

TWIG
{% block before_content %},{% endblock %}
<div class="row">
   <div class="large-12 columns">
      {% block content %},{% endblock %}
   </div>
</div>

How can I tell my slider's content it should render in 'before_content' instead of 'content'?

10 years ago

Sounds like you might want to look at modular page types for your homepage. That way you can build a page with multiple markdown files for different areas of your homepage, each with their own twig file to render. Check out the One-Page Skeleton in the downloads for a good working example of how this is achieved.

10 years ago

Still having a problem to wrap my head around. I can't get over it that there SEEMS to be only the {{ page.content }} variable where everything from the folder 'pages' goes. Am I right about that?

I managed to solve my original issue with a modular page but now I am facing a new one. In my footer, which is global, I want to place an additional section which should be fillable by markdown like all the other content in the 'pages' folder.

I don't want to hardcode content in a twig template. Template and content should stay separate. So how can I render content for the footer that is NOT part of {{ page.content }}?

10 years ago

stick your code into a footer partial. Have a look at the Bones Skeleton , they have a good example how this can be achieved.

10 years ago

Alright!

TWIG
{% block footer %}
        {% set content = pages.find('/footer').content %}
        {% if content %}
        <footer id="footer">
            <div class="row sg-center">
                 {{ content }}
            </div>
        </footer>
        {% endif %}
{% endblock %}

The magic is to pull the content into a variable first. Seems a little clumsy but will do the trick. This is the official way?

10 years ago

Yup, that's perfectly legit! Might wan to use another variable so it doesn't conflict with the content that is set in Twig vars, but other than that it's good.

10 years ago

Thanks! That's what i needed. I slightly changed the code to have the if statement test the "Page" instead of the content, makes more sense to me to do it this way.

TWIG

{% set footerpage = pages.find('/footer') %}
{% if footerpage %}
   {{ footerpage.content }}
{% endif %}
---

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1357 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago