Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-08T19:46:06.477Z
Answers.Text: Sorry. I wrote this before reading the comments above.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-08T19:47:14.573Z
Answers.Text: 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) }}
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-08T19:44:43.870Z
Answers.LastActivityDate: 2014-10-08T19:44:43.870Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-08T19:49:36.760Z
Answers.LastActivityDate: 2014-10-08T19:49:36.760Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-08T19:40:45.533Z
Comments.Text: I assume both arg1 and arg2 are required?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-08T19:42:24.363Z
Comments.Text: @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.
Comments.UserDisplayName: Adam McCombs
Tags: <plugin-development><plugins><variable>
Title: What's the best way to degrade gracefully when using a Plugin Variable?
ThreadText: # 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**: @username_1 - 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 %}
      <p class="notice">Display notice.</p>.
  {% 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) }}  
  
  ---
  
  