Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to call config.plugins.plugin-name.enabled in a template because of hyphen (-) character #1957

Closed
diazwatson opened this issue Mar 31, 2018 · 8 comments
Labels

Comments

@diazwatson
Copy link
Contributor

diazwatson commented Mar 31, 2018

I am trying to get the value of config.plugins.disqus-comments in a twig template but I always get null although if I dump the content of config.plugins I can see disqus-comments is there.

I also noticed that if I rename my plugin files and remove the hyphen (-) in the name, it works just fine.

Is this a bug?
Is there a way to scape this character in a twig template?

I would not like to have to change my plugin name just because of that :(

Thanks

@diazwatson
Copy link
Contributor Author

diazwatson commented Mar 31, 2018

I think I found a way around.
I am making available the configuration in twig templates declaring a variable and assigning to it.

    public function onTwigSiteVariables()
    {
        $this->grav['twig']->disqus_comments_plugin = $this->config->get('plugins.disqus-comments');
    }

@rhukster
Copy link
Member

Or use twigs attribute() function. That’s what it’s for.

@diazwatson
Copy link
Contributor Author

Hi Andy, thanks for your reply.
Is there an example on how to access plugin configuration using the attribute() function I can look at?

@rhukster
Copy link
Member

rhukster commented Apr 1, 2018

{% set some_var = attribute(config.plugins, 'disqus-comments').some_var %}

@rhukster rhukster closed this as completed Apr 1, 2018
@NicoHood
Copy link
Contributor

NicoHood commented Nov 5, 2020

A big ouuf to all plugins with dashed in their name. Accessing variables via page.header becomes very ugly now. Can't we at least have a syntax like page.header['dash-plugin].property? I remember there was a similar syntax for accessing configuration settings, but not for the page header. So I guess the original issue is fixed for plugin configs, but not for the page header/frontmatter.

Something like

return new Data((array)$this->header);

Here:
https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Page/Page.php#L462

It could also make setting new header variables easier by accessing header.variable.set('newvalue'). However I am not exactly sure how to implement this properly, so it works correct without side effects. @rhukster should know best.

@rhukster
Copy link
Member

rhukster commented Nov 5, 2020

If you are talking about Twig, it's a limitation in Twig itself. You should use the attribute() function: https://twig.symfony.com/doc/2.x/functions/attribute.html

I see I just repeated myself :)

@NicoHood
Copy link
Contributor

NicoHood commented Nov 6, 2020

Sure, but it seems you have fixed that for the config (original question by @diazwatson for this issue).

My request was to add this functionality to the page.header as well. Using the Data class this should be simple. I've tested this and it works, however you should know best if there are other side effects that we need to take care of, or if that fix is pretty straight forward. If it is easy, we should add it.

@rhukster
Copy link
Member

rhukster commented Nov 6, 2020

I don't see this change during the 1.x release. It would have too much compatibility impact. If you want to work with the hyphen easier, you can easily just convert the header array to a data object and work with it. The impact of that is minimal. I often do this in plugins when I need more flexibility that Data provides, and is a pain to work with arrays.

For Grav 2.x we'll definitely do things differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants