This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Kane
Kane

In my theme dir, /pages/about.htm

I'm trying to access a config setting, by using:

{{ Config::get('app.name') }}

However, I get the error: "Unexpected token "punctuation" of value ":" ("end of print statement" expected)."

I read https://github.com/rcrowe/TwigBridge and it recommended the config_get function, but that didn't work either.

I think my problem is similar to https://octobercms.com/forum/post/how-to-get-config-settings-in-to-the-theme?page=1 but there's no solution in that thread.

Any ideas?

DMeganoski
DMeganoski

Well it's not an elegant solution, but you could simply do


     ?php Config::get('app.name') ?>

For some reason it's stripping the first <

Last updated

Kane
Kane

Is there some place in October where config settings, such as the name of the app, should go?

DMeganoski
DMeganoski

Hmm... I see what you are aiming at. There is no config variable for that, the title of the page is determined by what you put into your theme files.

You can however, add config variables of your own.

Just open /config/app.php and add your own variable. i.e.


 /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => array_merge(include(base_path('modules/system/aliases.php')), [

        // 'Str' => 'Illuminate\Support\Str', // Example
        'Debugbar' => 'Barryvdh\Debugbar\Facade'

    ],
/*
* Custom Var
*/
'name' => 'This is my app'
),

Then you can access it via Config::get('app.name')

Kane
Kane

That's exactly what I did. See my original post, I get an error :)

So there's no 'elegant' way in October to set custom config settings and retrieve them in pages?

DMeganoski
DMeganoski

Ah, okay. I was not sure. Well, the chances of there being a twig function for this already are pretty good, but I wouldn't know where to start looking.

You can add your own twig function, say

config_get()

Here is the documentation on registering twig extensions in october: http://octobercms.com/docs/plugin/registration#extending-twig

So basically add this to your plugin.php


public function registerMarkupTags() {
        return [
            'functions' => [
                'config_get' => ['October\Rain\Support\Facades\Config', 'get']
            ]
        ];
    }

and then you should be able to use this in twig


{{ config_get('app.name') }}

Last updated

DMeganoski
DMeganoski

actually, the class you need to reference is probably October\Rain\Config\Repository not the Facade.

Kane
Kane

Yeah that doesn't work. Thanks for your efforts though, I'll just continue to hard-code the app name all over the place lol.

Eoler
Eoler

Kane said: Is there some place in October where config settings, such as the name of the app, should go?

Usually in plugin config (if plugin represents "application"): https://octobercms.com/docs/plugin/settings#file-configuration but for this use-case you can also rename theme (theme.yaml or backend UI) and display it in a page/Twig with:


{{ this.theme.getConfigValue('name') }}
Kane
Kane

^ I like this suggestion, thanks!

GrCOTE7
GrCOTE7

Me too... Thanks, Eoler :-)

<?php
$pseudo = 'Eoler';
echo 'You\'re great, ' . $pseudo;
?>

Last updated

bugzbrown36350
bugzbrown36350

I know this is quite old, but I ran into the same problem. I could not find an answer anywhere, so I came up with a solution using a twig function:

https://gist.github.com/bugzbrown/118a79322c00862a07f52e75705e23b7

// In you plugin file add:
public function registerMarkupTags()
{
    return [
        'functions' => [
            'getSettings' => function($setting, $default)
            {
                return Settings::get($setting, $default);
            }
        ]
    ];
}

Then you can access your settings on partials or pages using:

{{ getSettings('mySetting','Default Value if not set') }}

Hope this comes in handy to somebody.

thedesigncompany
thedesigncompany

bugzbrown36350 said:

I know this is quite old, but I ran into the same problem. I could not find an answer anywhere, so I came up with a solution using a twig function:

https://gist.github.com/bugzbrown/118a79322c00862a07f52e75705e23b7

This made everything a lot easier, Thanks! A suggestion, changing:

'getSettings' => function($setting, $default)

into

'getSettings' => function($setting, $default = 'no value set')

Will make the second parameter optional.

Last updated

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.