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

Access to page.media.images in for-loop

Started by Muut Archive 10 years ago · 1 replies · 416 views
10 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.

10 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 Deleted User, 9 years ago
0 1366 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago