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);
Phil Gyford
  • 667
  • 1
  • 6
  • 17

3 Answers3

6

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
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
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 %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
2

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 :/

Alec Ritson
  • 4,529
  • 1
  • 19
  • 34