<p>How are you trying to use it in the template? </p>

<p>Below is a very basic way of how to display a setting in a front end twig template.</p>

<p>First define your settings with the defineSettings() method (as you already have).</p>

<pre><code>protected function defineSettings()
{
    return array(
        'mySetting' =&gt; array(AttributeType::String, 'required' =&gt; true)
    );
}
</code></pre>

<p>Then create a directory in the root of your plugin folder and call it “templates”. Create a new .html or .twig file in that folder and call it “_settings.html”. Inside of that you could put the following: </p>

<pre><code>{% import "_includes/forms" as forms %}
{{ forms.textField({
        label: "My Setting"|t,
        id: 'mySetting',
        name: 'mySetting',
        instructions: "Input your setting"|t,
        value: settings.mySetting,
        errors: settings.getErrors('mySetting')
    }) 
}}
</code></pre>

<p>After this go back to your primary plugin class and add the getSettingsHtml() method:</p>

<pre><code>public function getSettingsHtml()
{
    return craft()-&gt;templates-&gt;render(‘pluginHandle/_settings', array(
        'settings' =&gt; $this-&gt;getSettings()
    ));
}
</code></pre>

<p>Now, if you go into admin/settings/plugins you can click on your plugin and there will be an input field there where you can save the setting. If you wanted to get this value you could add a method to your <a href="http://buildwithcraft.com/docs/plugins/services">service</a> like this:</p>

<pre><code>public function getMySetting()
{
    $plugin = craft()-&gt;plugins-&gt;getPlugin(‘pluginHandle');
    $settings = $plugin-&gt;getSettings();

    return $settings-&gt;mySetting;
}
</code></pre>

<p>You could call this anywhere throughout your plugin like this:</p>

<pre><code>craft()-&gt;pluginHandle_serviceName-&gt;getMySetting(); 
</code></pre>

<p>To do that on the front end in a twig template, just create a method in the <a href="http://buildwithcraft.com/docs/plugins/variables">variables class</a>. It would look something like this:</p>

<pre><code>public function getMySetting()
{
    return craft()-&gt;pluginHandle_serviceName-&gt;getMySetting();
}
</code></pre>

<p>In your front end twig template you could call it like this; </p>

<pre><code>{{ craft.pluginHandle.getMySetting() }}
</code></pre>

<p>I hope any of this clears it up a bit. </p>

CC BY-SA 3.0
2015-02-06T21:39:32.117Z
This is really useful it would be great if the plugin settings doc page had more on it like this
mattl
CC BY-SA 3.0
2014-12-04T04:43:17.820Z
2014-12-04T04:43:17.820Z
Matt Green
<p>I am learning craft and trying add settings page for my plugin. I read that need to define settings like,</p>

<pre><code>class CocktailRecipesPlugin extends BasePlugin
{
    // ...

    protected function defineSettings()
    {
        return array(
            'cocktailCategories' =&gt; array(AttributeType::Mixed, 'default' =&gt; array('Sours', 'Fizzes', 'Juleps')),
        );
    }
}
</code></pre>

<p>So isn't clear and there is no explanation what the array return and how to use it inside the template. </p>

<p>Thank you </p>

<plugin-development><templating>
How to use plugin settings
# How to use plugin settings

(Asked by: username_0 on 2014-12-03)

I am learning craft and trying add settings page for my plugin. I read that need to define settings like,
class CocktailRecipesPlugin extends BasePlugin
{
    // ...

    protected function defineSettings()
    {
        return array(
            'cocktailCategories' => array(AttributeType::Mixed, 'default' => array('Sours', 'Fizzes', 'Juleps')),
        );
    }
}

So isn't clear and there is no explanation what the array return and how to use it inside the template. 
Thank you

**Tags:** plugin-development, templating


---

## 1 answers

### Answer by username_1 on 2014-12-04. Score: 25 | Accepted answer

How are you trying to use it in the template? 
Below is a very basic way of how to display a setting in a front end twig template.
First define your settings with the defineSettings() method (as you already have).
protected function defineSettings()
{
    return array(
        'mySetting' => array(AttributeType::String, 'required' => true)
    );
}

Then create a directory in the root of your plugin folder and call it “templates”. Create a new .html or .twig file in that folder and call it “_settings.html”. Inside of that you could put the following: 
{% import "_includes/forms" as forms %}
{{ forms.textField({
        label: "My Setting"|t,
        id: 'mySetting',
        name: 'mySetting',
        instructions: "Input your setting"|t,
        value: settings.mySetting,
        errors: settings.getErrors('mySetting')
    }) 
}}

After this go back to your primary plugin class and add the getSettingsHtml() method:
public function getSettingsHtml()
{
    return craft()->templates->render(‘pluginHandle/_settings', array(
        'settings' => $this->getSettings()
    ));
}

Now, if you go into admin/settings/plugins you can click on your plugin and there will be an input field there where you can save the setting. If you wanted to get this value you could add a method to your service like this:
public function getMySetting()
{
    $plugin = craft()->plugins->getPlugin(‘pluginHandle');
    $settings = $plugin->getSettings();

    return $settings->mySetting;
}

You could call this anywhere throughout your plugin like this:
craft()->pluginHandle_serviceName->getMySetting(); 

To do that on the front end in a twig template, just create a method in the variables class. It would look something like this:
public function getMySetting()
{
    return craft()->pluginHandle_serviceName->getMySetting();
}

In your front end twig template you could call it like this; 
{{ craft.pluginHandle.getMySetting() }}

I hope any of this clears it up a bit.

> **Comments:**
> **username_2**: This is really useful it would be great if the plugin settings doc page had more on it like this  

---

