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

Global twig variable for page.find('/images')

Solved by Sebastian View solution

Started by Sebastian 8 years ago · 6 replies · 1547 views
8 years ago

Hey guys,

when using a centralized folder for image linking like the learn page suggests, is it somehow possible to store the image path for "page.find('/images')" in a global twig variable?

right now I always have to set the image path on each template, like this:
{% set images = page.find('/images) %}}
{ images.media['my-image.jpg'].html }}

That's redundant...

My idea is to define the set images variable somewhere global, to use is in multiple templates without defining it each time.

Maybe this is possible by extending the twig_vars in my theme.php, but I'm not sure if that's the right approach?

8 years ago

So have you tried setting it in your theme's base template? I keep forgetting how scope works in Twig. I assume you have, though.

Failing that, twig_vars as you mentioned will work and is the only other option I can think of.

👍 1
8 years ago

Setting a global var inside the base template was one of the first things I tried. Unfortunately it does not work.

According to this answer it's not possible to do this template-wise, without passing the variable as an argument when including partials. (/forum/archive/dynamic-global-variable-t3501)

Extending twig_vars inside my theme.php does work in fact. I just had a look at how it's done at Gravs' default theme quark, but I don't know how to get page.find('/images') to work inside the theme.php

8 years ago

I would 😍 to see that feature implemented.

👍 1
8 years ago

It's not as well documented and often requires experimenting to see how it actually works, but if you are comfortable digging around in the API docs and sometimes source code, you'll find that most Grav Twig functions have a PHP equivalent. In fact, I think the Twig API simply calls the PHP API and is a subset of it.

Looks like there's a PHP find method on the Page object and Pages object and possibly other useful methods. Try those and look further if you need to.

👍 1
8 years ago

Ahh!Almost forgot about the api section, will have a look and hopefully get it to work somehow.
thanks!

8 years ago Solution

@hughbris

OK, got it working!
My theme.php looks like this now:

PHP
<?php
namespace Grav\Theme;

use Grav\Common\Theme;
use Grav\Common\Grav;

class THEME_NAME extends Theme
{

    public function onTwigInitialized() {
        $grav = Grav::instance();
        $grav['pages']->init(); // initializing pages to access them
        $twig = $this->grav['twig'];

        $imagePath =  $grav['pages']->find('/images');
        $globalVars = [
            'images' => $imagePath,
        ];

        $twig->twig_vars = array_merge($twig->twig_vars, $globalVars);

    }
}

You just have to initialize Pages before you can use them, then you can use the find(string $url, bool $all=false) function from the pages API to get the images location.
After this, I just can use my custom global var images in every twig template file, like this:

HTML
    <figure class="w100 w8@l overlay pr@l">
        {{ images.media[page.header.heroimage].cropResize(hero_image_width, hero_image_height).html('title', 'ALT text', 'db fit-cover') }}
    </figure>

Maybe some PHP/Twig expert can still have a look at it, maybe there is a better way to write this.
However… at least it works for me now.

Again thanks for pointing me in the right direction. Cheers 🎉

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago