<p>You can use the <code>is defined</code> twig expression to determine if a variable is defined or not. Something like this:</p>

<pre><code>{% if craft.pluginname.variable(arg1, arg2) is defined %}
    {{ craft.pluginname.variable(arg1, arg2) }}
{% elseif %}
    &lt;p class="notice"&gt;Display notice.&lt;/p&gt;.
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-10-08T19:46:06.477Z
Sorry. I wrote this before reading the comments above.
Douglas McDonald
CC BY-SA 3.0
2014-10-08T19:47:14.573Z
This is good but I'd like it to be self contained within the plugin so you would only need to include {{ craft.pluginname.variable(arg1, arg2) }}
Adam McCombs
CC BY-SA 3.0
2014-10-08T19:44:43.870Z
2014-10-08T19:44:43.870Z
Douglas McDonald
<p>I think the best way to do this would be in your <code>PluginHandleVariable</code> file's method, just have it accept one, optional parameter and expect it to be an array.</p>

<pre><code>public function variableMethod($options = null)
{
    if (!$options || !is_array($options))
    {
        return "No options set";
    }

    // Now you can check for individual options on the array
    if (isset($options['option1'])
    {
        ....
    }

}
</code></pre>

<p>Then both of these would work:</p>

<pre><code>if craft.pluginname.variable()
if craft.pluginname.variable(options)
</code></pre>

CC BY-SA 3.0
2014-10-08T19:49:36.760Z
2014-10-08T19:49:36.760Z
Brad Bell
<p>When providing plugin users with variable containing arguments like <code>{{ craft.pluginname.variable(arg1, arg2) }}</code> what's the best practice for returning something that doesn't throw an Internal Server Error exception for Missing argument 1 or argument 2?</p>

<p>Should this be done as part of the /services/PluginService.php or somewhere else? Is it better to return a nicer Javascript console error (or Craft error log) vs. crashing the page?</p>

CC BY-SA 3.0
2014-10-08T19:40:45.533Z
I assume both arg1 and arg2 are required?
Brad Bell
CC BY-SA 3.0
2014-10-08T19:42:24.363Z
@BradBell - lets say yes. If not set, I'd like to return a nice "no options set" error and an empty output vs. the Internal Server Error crash.
Adam McCombs
<plugin-development><plugins><variable>
What's the best way to degrade gracefully when using a Plugin Variable?
# What's the best way to degrade gracefully when using a Plugin Variable?

(Asked by: username_0 on 2014-10-08)

When providing plugin users with variable containing arguments like `{{ craft.pluginname.variable(arg1, arg2) }}` what's the best practice for returning something that doesn't throw an Internal Server Error exception for Missing argument 1 or argument 2?

Should this be done as part of the /services/PluginService.php or somewhere else? Is it better to return a nicer Javascript console error (or Craft error log) vs. crashing the page?

**Tags:** plugin-development, plugins, variable

> **Comments:**
> **username_1**: I assume both arg1 and arg2 are required?  
> **username_0**: @BradBell - lets say yes. If not set, I'd like to return a nice "no options set" error and an empty output vs. the Internal Server Error crash.  

---

## 2 answers

### Answer by username_1 on 2014-10-08. Score: 6 | Accepted answer

I think the best way to do this would be in your PluginHandleVariable file's method, just have it accept one, optional parameter and expect it to be an array.
public function variableMethod($options = null)
{
    if (!$options || !is_array($options))
    {
        return "No options set";
    }

    // Now you can check for individual options on the array
    if (isset($options['option1'])
    {
        ....
    }

}

Then both of these would work:
if craft.pluginname.variable()
if craft.pluginname.variable(options)


---

### Answer by username_2 on 2014-10-08. Score: 1

You can use the is defined twig expression to determine if a variable is defined or not. Something like this:
{% if craft.pluginname.variable(arg1, arg2) is defined %}
    {{ craft.pluginname.variable(arg1, arg2) }}
{% elseif %}
    Display notice..
{% endif %}

> **Comments:**
> **username_2**: Sorry. I wrote this before reading the comments above.  
> **username_0**: This is good but I'd like it to be self contained within the plugin so you would only need to include {{ craft.pluginname.variable(arg1, arg2) }}  

---

