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.

General

How to reference hero image in the images file

Solved by pamtbaau View solution

Started by Norma White 8 years ago · 11 replies · 4717 views
8 years ago

I have a site that successfully loads a hero image when the image is in the same page
eg. hero_image: small-bandpic.png
But I want to use this image as hero for several pages.
So I put it in user/images
then hero_image: images/small-bandpic.png fails to find the image
and so does hero_image: user/images/small-bandpic.png

I tried with quotes but the administrator removed them.

Does anyone have better ideas?
(I am using quark theme, fully updated and yes, I cleared cache and browser cache.)
Thanks
Norma

8 years ago Solution

@normawhite I have never used Quark but had a look at the README and code.

The README.md says explicitly:

The hero_image should point to an image file in the current page folder

And also the code looks for an image in the folder of the page. For example, the twig templates/item.html.twig looks for the image with:

{% set hero_image_name = page.header.hero_image %}
...
{% set hero_image = page.media[hero_image_name] %}

If you want the template to find an image in the pages/images folder, do the following:

  • In the page's frontmatter do:

    hero_image: small-bandpic.png

    Note: No path prepended

  • In the template item.html.twig file:

    {% set hero_image = page.find('/images').media[hero_image_name] %}

    See the Grav docs in Where to put your media files

last edited 06/26/18 by pamtbaau
8 years ago

Thanks. I read so many docs that I forgot about this "little" thing:)

8 years ago

@normawhite If you make changes, don't forget to read about Theme inheritance. If you make changes in Quark itself, your changes will be overridden when installing a new release of Quark.

8 years ago

I temporarily got my site going with a copy of a hero image in about 5 folders, but now I am trying to get page.find to find it either in the current folder or in /images. page.find does not find anything.

I have a modified copy of hero.html.twig in my theme. It is definitely is being executes. It works when the hero image is in the current folder but fails to find the image in 'images'.

Here is the code
hero.html.twig name {{page.header.hero_image}}
{% set grid_size = theme_var('grid-size') %}

{% set hero_image = page.media[page.header.hero_image] %}
{% if hero_image %}
Do nothing
{% else %}
Looking in images for {{page.header.hero_image}}
{% set hero_image = page.find('/images').media[page.header.hero_image] %}
Found {{hero_image.url}}
{% endif %}

{% include 'partials/hero.html.twig' with {id: 'modular-hero'} %}

And here is what it prints
Looking in images for small-bandpic.png Found
and it shows a blank picture.

Thanks
Norma

8 years ago

Hi @normawhite, long time no see...

Hate so say, but it seems to be working in my environment...

In 'One-Page Skeleton' which uses Quark, I did the following:

  • Using $ bin/plugin devtools newtheme I created an inherited theme from Quark
  • In '/user/config/system.yaml' I set 'theme: mytheme'
  • In '/user/config/system.yaml' I disabled cache: 'enabled: false'.
  • I copied the template 'user/themes/quark/templates/modular/hero.html.twig' into folder 'user/themes/mytheme/templates/modular/'
  • Replaced content of 'hero.html.twig' with the code you've given above.
  • Created folder '/user/pages/images'
  • Moved file 'header.jpg' from '/user/pages/01.home/01.hero/header.jpg' into '/user/pages/images'
  • Refreshed the browser:
    • It prints: (formatted)
      TXT
      hero.html.twig name header.jpg 
      Looking in images for header.jpg 
      Found /site-modular/user/pages/images/header.jpg' 
      
    • and the hero images is shown correctly...
8 years ago

Many thanks - the problem was that my header image was in user/images and not user/pages/images. Without your help I doubt that I would have found that ever (though the manual is very clear).

7 years ago

Hi

I have the same or very similar problem. I spent about 5 hours browsing the web and trying. No success.

I am using the Skeleton ( Halcyon Site).

I wanted a hero carousel. I modified the header.twig.html with this:

HTML
<header id="home">
  {% block navigation %}
  {% include 'partials/navigation.html.twig' %}
  {% endblock %}
  <section class="hero" id="hero">
    <div>
      <div class="nav-hamburger">
        <div class="col-md-12 text-right navicon">
          <a id="nav-toggle" class="nav_slide_button" href="#"><span></span></a>
        </div>
      </div>

      <div id="hero-carousel" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
          <ol class="carousel-indicators">
            <li data-target="#hero-carousel" data-slide-to="0" class="active"></li>
            <li data-target="#hero-carousel" data-slide-to="1"></li>
            <li data-target="#hero-carousel" data-slide-to="2"></li>
          </ol>

          <!-- Wrapper for slides -->
          <div class="carousel-inner" role="listbox">
            <div class="item active">
              <img src="https://via.placeholder.com/1600x800?text=SLIDE+1" alt="...">
              <div class="carousel-caption">
                <h5>SkiGUlmarg.com</h5>
                <p>
                  Vist Skigulmarg to live the ski trip of YOUR life time and ski the HIMALAYAS!
                </p>
                <div class="button">
                  Visit Skigulmarg.com
                </div>

              </div>
            </div>
            <div class="item">
              <img src="https://via.placeholder.com/1600x800?text=SLIDE+2" alt="...">
              <div class="carousel-caption">
                <h5>SkiGUlmarg.com</h5>
                <p>
                  Vist Skigulmarg to live the ski trip of YOUR life time and ski the HIMALAYAS!
                </p>
                <div class="button">
                  Visit Skigulmarg.com
                </div>
              </div>
            </div>
            <div class="item">
              <img src="https://via.placeholder.com/1600x800?text=SLIDE+3" alt="...">
              <div class="carousel-caption">
                <h5>SkiGUlmarg.com</h5>
                <p>
                  Vist Skigulmarg to live the ski trip of YOUR life time and ski the HIMALAYAS!
                </p>
                <div class="button">
                  Visit Skigulmarg.com
                </div>
              </div>
            </div>
          </div>
      </div>
    </div>
</section>
</header>

I added a bit of css.

Now what I am trying to do is use something like this (From: https://learn.getgrav.org/16/content/media)

TWIG
```
{{ page.media['retina.jpg'].sizes('80vw').html() }}
``` 

For Grav to create a srcset and the images.

I also want to have my [email protected] in user/pages/images folder.

I want to add reference to these images in the frontmatter/header so the name of the images is not hardcoded in the header.twig.html.

I tried so many ways but. no success. I had one way that did what I wanted by adding the images in the file media then using twig filters |first and |last

TWIG
{{ page.media|fisrt.sizes('100vw').html() }}
{{ page.media|last.sizes('100vw').html() }}

But the if I had more then 2. I did no manage to access the second or...

I trised |slice(1,1)

No luck.

I tried, as shown in the tread above:

HTML
<div class="item active">
              {% set image_one_name = page.header.image_one %}
              {% set image_one = page.find('/images').media[image_one_name] %}
              {{ page.media[imageone].sizes('100vw').html('','my sushi alt') }}
              <div class="carousel-caption">
                <h5>SkiGUlmarg.com</h5>
                <p>
                  Vist Skigulmarg to live the ski trip of YOUR life time and ski the HIMALAYAS!
                </p>
                <div class="button">
                  Visit Skigulmarg.com
                </div>

              </div>
            </div> 

With this in my page frontmatter

YAML
imageone: s[email protected]
image2: taroko.jpg
image3: hot.jpg
alt:
    alt1: 'this is my alt'
    alt2: 'this is my alt 2'
    alt3: 'this is my alt 3'

Any help would be appreciated.

Thanks in advance.

last edited 10/03/19 by Yanik Turgeon
7 years ago

@Boaime, There are a couple of issues in both your frontmatter as in Twig:

  • Variable names in Twig and frontmatter do not match, e.g. 'imageone' vs. 'image_one'
  • Referencing location of media files in Twig is not consistent. You refer to two different folders:
    • 'pages/images' folder by using page.find('/images').media[image_one_name]
    • 'pages/<currentpage>/' folder by using page.media[imageone]

Here is a working sample, where all images are stored in '/user/pages/images':

frontmatter:

YAML
slides:
    - name: sushi.jpg    # Note: The file does not have any density info
      alt: 'this is my alt'
    - name: taroko.jpg
      alt: 'this is my alt 2'
    - name: hot.jpg
      alt: 'this is my alt 3'

Twig:

TWIG
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
  {% for slide in page.header.slides %}
    {% set image = page.find('/images').media[slide.name] %}
    <div class="item {{ loop.first ? 'active' : '' }}">
        {{ image.sizes('100vw').html('', slide.alt) }}
        <div class="carousel-caption">
          ... 
        </div>
    </div>
  {% endfor %}
</div>

'/user/pages/images/' folder:

TXT
[email protected]  // Note the density indication
hot,jpg
taroko.jpg

This will generate the following image elements:

With denisity:

TXT
<img alt="this is my alt" src="/grav/site-halcyon/images/3/3/c/a/3/33ca33e5eb2c1b2213c51989304f8f18315df896-sushi1x.jpeg" srcset="/grav/site-halcyon/user/pages/images/[email protected] 4928w, /grav/site-halcyon/images/b/1/c/2/2/b1c22f78b42f13534992253fbc5c0eb66e68c084-sushi2x.jpeg 3285w, /grav/site-halcyon/images/3/3/c/a/3/33ca33e5eb2c1b2213c51989304f8f18315df896-sushi1x.jpeg 1642w" sizes="100vw">

Without density:

TXT
<img alt="this is my alt 2" src="/grav/site-halcyon/user/pages/images/taroko.jpg">

Apart from the coding issues, I think the missing link was this note in the documentation:

Let's assume you have a file called [email protected] , you would actually reference this in your links as retina.jpg [...]

This means, the file itself contains the density in its name, but file references in frontmatter/Twig do not contain density info.

Hope this helps...

👍 1
7 years ago

@Boaime, I think this question should be in a separate issue. If you create a new issue, I'll be happy to copy my reply.

@hughbris, Do you perhaps have the permission to split this issue into its own with a few keystrokes? That would be the easiest way I guess.

7 years ago

That doesn't seem to be an option for me.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 74 6 hours ago
General · by pamtbaau, 12 hours ago
1 47 11 hours ago
General · by Andy Miller, 24 hours ago
0 44 24 hours ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 39 5 days ago