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

Dynamic variable in page

Started by Muut Archive 10 years ago · 5 replies · 459 views
10 years ago

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:

PHP
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.

10 years ago

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

10 years ago

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.

10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1350 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4061 9 years ago
Archive · by Muut Archive, 9 years ago
1 2948 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago