Out of memory with config variable

Im trying to set a config variable with the site title so that I can reuse it later a few times in the config & templates. Trouble is, it keeps running out of RAM, and i’m not sure why. bug perhaps?

I set a variable like this, at the top of the config…

'forge.sitetitle' => site()->title()->html(),

And then use it later on, in the metatags plugin for example…

'pedroborges.meta-tags.default' => function ($page, $site) {

    return [
      'title' => $page->isHomePage()
        ? option('forge.sitetitle')
        : $page->seotitle().' | '.option('forge.sitetitle'),
            ....

I’m using it 4 or 5 times, but the site just hangs and eventually tells me the memory is exhausted. Is it a bug or me? Kirby 3.1.2.

Oddly, if i use site()->title()->html() multiple times in the config, it’s happy. If i call the value from the option, it runs out of ram.

You probably create a loop when calling an option within the options.

Any way round it?

I’ve run into this as well. It looks like the issue is that calling site() from the config loads the Kirby installation again, which loads the config, which calls site(), which loads the installation again, which loads the config and on and on. Because that creates a lot of PHP objects, memory is exhausted.

I have created an issue here. I can’t promise if there can be a fix (using the $site object from the config was always difficult as the object and all of its child objects depend on the config), but we will take a look at it.

Smashing… thanks @lukasbestle :slight_smile:

Would it help if we used a closure? Like this:

'forge.sitetitle' => function() { return kirby()->site()->title()->html(); },

I basically tried that… it got ugly :frowning: i did this…

'forge.sitetitle' => function($site) { return $site->title()->html(); }

I’m not completely getting your use case tbh. Why do you want to store something which is in $site in an option?

$site is basically available everywhere, right? So you could use following in the meta-tags config:

'pedroborges.meta-tags.default' => function ($page, $site) {
    $siteTitle = $site->title()->html();

    return [
      'title' => $page->isHomePage()
        ? $siteTitle
        : $page->seotitle().' | '.$siteTitle,
            ....

Welllll I could I guess… but I use it in a few places, including stuff like the copyright line in the sites footer. It just seemed to me like a simple way to put it in one place and have it update in a dozen others from one line. I wouldnt say im doing something unreasonable :slight_smile:

And occasionally I might want to to get clever with it, like concatenate it with a another string or something. I try to go for having to edit the least amount of code.

Ok, gotcha! It’s more about the principle then :slight_smile:

I only wondered what the gain was between option('forge.sitetitle') and $site->title()->html() :man_shrugging:.

This thing is something I use as a base for all my sites, so I try to keep the maintenance overhead as low as possible. That way i’m just building the 20% thats particular to the current project, and not repeating effort.

From Kirby 3.3.0 you can use the ready config option to set last minute options that require the Kirby object.

1 Like