Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Access to page.media.images in for-loop

Started by Muut Archive 11 years ago · 1 replies · 569 views
11 years ago

Hi,

this is my first post here and I have a question where I hope that it is not too stupid.

My problem is the following: I wanted to extend the modular-element "features" (from the default antimatter-theme) with an individual background-image for each feature I insert. ok, file upload does not work, so I fell back to pagemediaselect in my YAML, which worked so far. The image that i choose in the dropdown is associated to the feature and its filename is accessible in the for loop:

TWIG
{% for feature in page.header.features %}
   <div style="background-image: url('{{ feature.background }}');">
      some content here
   </div>
{% endfor %}

produces:

HTML
<div style="background-image:url('myimage.jpg');">
   some content here
</div> 

But here comes the tricky part:

That is not enough - the background image will not be shown because in that case it relates to the base_url (http://localhost/tests/grav-dev/myimage.jpg).

I tried another approach:

TWIG
{% for feature in page.header.features %}
   <div style="background-image: url('{% page.media.images['{{ feature.background }}'].url % }');">
      some content here
   </div>
{% endfor %}

which produces:

HTML
<div style="background-image:url('');">
   some content here
</div>

right - nothing.

What am I doing wrong here? Or is there another way to reference page images in for loops for blueprinted elements?

Thanks in advance.

11 years ago

never mind - found out by myself.

TWIG
<div style="background-image: url('{{page.url}},{{feature.background}}')">
   some content here
</div>

did the trick ...

Suggested topics

Topic Participants Replies Views Activity
Archive · by Muut Archive, 10 years ago
1 676 1 week ago
Archive · by Deleted User, 9 years ago
0 3043 9 years ago
Archive · by Muut Archive, 9 years ago
2 1983 9 years ago
Archive · by Muut Archive, 9 years ago
2 5337 9 years ago
Archive · by Muut Archive, 9 years ago
1 4134 9 years ago