Hello,
I wish create some text with variables like :
// config/annonces.yaml
region: "My region"
// config/news.yaml
presentation: My text blablabla <em>lbablaba {{ config.annonces.region }}</em> blabla ...
Is it possible ? Thx
Hello,
I wish create some text with variables like :
// config/annonces.yaml
region: "My region"
// config/news.yaml
presentation: My text blablabla <em>lbablaba {{ config.annonces.region }}</em> blabla ...
Is it possible ? Thx
See the docs:
You are also not limited to the
user/config/system.yaml
or theuser/config/site.yaml
files as described above. You can create any arbitrary.yaml
configuration file in theuser/config
folder you wish and it will get picked up by Grav automatically.
I do not believe thereâs a way to reference other YAML files from within YAML. There are only node anchors. The YAML parser certainly wonât parse the Twig. But I suppose if you inject the Twig early enough in the lifecycle, it will end up being processed when the page is.
I found the following in the Twig docs:
{{ include(template_from_string("Hello {{ name }}")) }}
It seems this is what you are after, but I couldnât get it to workâŚ
Prerequisite:
The
template_from_string
function is not available by default. You must add theTwig_Extension_StringLoader
extension explicitly when creating your Twig environment
@nicolasG Did some more research and managed to get the suggestion I made above workingâŚ
Iâm using the Quark theme for convenience, but an inherited theme should be used for production.
These are the steps I took:
region: "My region"
presentation: My text blablabla <em>lbablaba {{ config.annonces.region }}</em> blabla ...
public function onThemeInitialized()
{
}
with:public function onThemeInitialized()
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
// Enable the main event we are interested in
$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 0],
]);
}
public function onTwigExtensions()
{
$this->grav['twig']->twig->addExtension(new \Twig_Extension_StringLoader());
}
<h1>{{ include(template_from_string(config.news.presentation)) }}</h1>
My text blablabla lbablaba My region blabla âŚ
I didnât know where to put âTwig_Extension_StringLoaderâ, your answer provides the solution, perfect, thank you very much!
Now, it opens a debate on the solution to be adopted⌠Let me explain:
Either this solution, or create a twig function that makes a simple âstr_replaceâ of keywords containing in the text as %%city%%%% then in my html view, just do {{ replaceAndShowFunction(config.news.presentation) }}, what do you think?
Which solution would you prefer?
It depends on your decisive selection criteria of the solution. Only then can be decided which solution fits best.
I donât know yours, but I could think of criteria like: