Dynamic variable in page

Hi all,

This may sound quite basic, but I haven’t found the solution after looking around for a while and testing several options.

I need to pass a dynamic variable to a page, with a value that’s coming from an API, something like this:

public static function getNumber() {
    $number = '123'; // Code to obtain number
    return $number;
}

But not sure if this actually works as I haven’t found a way to pass this to the page content. I’d like this number to appear within the copy, so it has to be passed as a variable to the page content.

Any pointers?

Thanks.

It depends on when the API is called. If the call happens in a template, you can pass it to other templates with the with keyword. If the call happens in a plugin, checkout out the Learning by Example section about this.

Hi, Thanks for your answer, I will check on the links. But please not that I need to pass this variable to the page content (.md file), not to the template, so I can add it to the front matter of the page and then in the content like “…lorem ipsum dolor {{ page.header.number }} sit met consectetur…”

Is this possible?

Thanks

I’ve managed to make my theme generate a variable which is accessible in the templates with $this->grav['twig']->twig_vars['variable'] = $data; and using it like {{ variable }} but I have not been able to pass this to the page content.

You just have to enable twig in the page and then use the variable as usual.

That was it! I had it the wrong way before and that’s why it didn’t work. Thanks a lot!