2

This question and answer explains how to access a plugin's settings from elsewhere in the plugin's PHP code.

How would I access these settings from within a template? Or this impossible and I'd have to pass the values into the template when rendering it? ie, in a Controller action:

$settings = craft()->plugins->getPlugin('myPlugin')->getSettings();
$templateVars = array(
    'foo' => $settings->foo,
);
$this->renderTemplate('myPlugin/templateName', $templateVars);
4

Not sure if there's a build in way, but you can set up a template variable that returns the settings model.

class MyPluginVariable
{
    public function settings()
    {
        return craft()->plugins->getPlugin('myPlugin')->getSettings();
    }
}

Then access it in your template.

{% set settings = craft.myPlugin.settings %}
  • Thanks Carl. Looks like that's the closest I'm going to get. – Phil Gyford Sep 3 '15 at 8:51
5

In more recent versions of Craft 2.x (I forget when it was added), you can simply do this without needing a plugin:

craft.config.get('setting', 'plugin')

Where 'plugin' is the name of your plugin's config file (sans .php).

If you want to do this in Craft 3, you'd do:

craft.app.getModule('pluginHandle').getSettings().mySetting
1

From what I understand the only way to get plugin info back (in a template) is by doing:

{% set plugin = craft.plugins.getPlugin('PluginHandle') %}

This returns an instance of PluginVariable for your plugin, so you have access to any method detailed in craft/app/variables/PluginVariable.php (the craft class reference)

It looks like the getSettings() function isn't exposed in this, so I don't think its possible.

I could be wrong and there could be a way, but I couldn't figure it out from a quick look over the code :/

  • Thanks Alec - that's as far as I'd got with looking around, so good to have it confirmed! – Phil Gyford Sep 3 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.