<p>You're close, you don't necessarily need another class or function. Though you may want to change the function name to be a bit more clear, and you may want to move the heavy XML logic into a separate private method.</p>

<pre class="lang-php prettyprint-override"><code>class AiPaginationTestVariable
{
        public function getXml($params = null)
        {
            // Compile XML
            $xml = $this-&gt;_compileXml($params);

            // Return XML or false
            if ($xml) {
                return $xml;
            } else {
                return false;
            }
        }

        private function _compileXml($params = null)
        {
            // Whatever it takes to compile XML
        }
}
</code></pre>

<p>Then in your Twig template, you can do something like this...</p>

<pre><code>{% set xml = craft.aiPaginationTest.getXml() %}

{% if false == xml %}
    {# Handle no XML #}
{% endif %}
</code></pre>

<p>This might not directly answer your question, but I hope it's enough to get  you back on track.</p>

CC BY-SA 4.0
2019-01-26T05:58:25.033Z
2019-01-26T05:58:25.033Z
Lindsey D
<p>I am working with a plugin and wish to return a value from it to the template that calls it.  In my plugin, there is an XML feed call, and I would like to pass a value(variable) from the plugin to the template that indicates whether there is any value for a particular XML query.  </p>

<p>I currently am working with a Plugin Variable, which returns the XML output.<br>
How would I be able to return a second value to the template for a conditional 'display/don't display' value?  Would I need to set up a second class, apart from my main XML feed variable?</p>

<p>Thanks.</p>

<p>Here is the basic layout of the plugin:</p>

<p>XmlFeedVariable.php</p>

<pre><code>&lt;?php
namespace Craft;

class AiPaginationTestVariable
{
        public function AiPaginationTestVariable($optional = null)
        //the '$optional' in the function call passes to the xml call for the query
            {
            //performs XML call and string assembly here, then echo's to output
            //if there is no xml returned, I would like to do this:
            //$xml_return='no';
            //then have the template pick it up.
            }
}
</code></pre>

<plugin-development><templating><variable><craft2>
Craft 2 - Returning Value in Variable from Plugin to Template
# Craft 2 - Returning Value in Variable from Plugin to Template

(Asked by: username_0 on 2019-01-26)

I am working with a plugin and wish to return a value from it to the template that calls it.  In my plugin, there is an XML feed call, and I would like to pass a value(variable) from the plugin to the template that indicates whether there is any value for a particular XML query.  
I currently am working with a Plugin Variable, which returns the XML output.
How would I be able to return a second value to the template for a conditional 'display/don't display' value?  Would I need to set up a second class, apart from my main XML feed variable?
Thanks.
Here is the basic layout of the plugin:
XmlFeedVariable.php

**Tags:** plugin-development, templating, variable, craft2

---

## 1 answers

### Answer by username_1 on 2019-01-26. Score: 1

You're close, you don't necessarily need another class or function. Though you may want to change the function name to be a bit more clear, and you may want to move the heavy XML logic into a separate private method.
class AiPaginationTestVariable
{
        public function getXml($params = null)
        {
            // Compile XML
            $xml = $this->_compileXml($params);

            // Return XML or false
            if ($xml) {
                return $xml;
            } else {
                return false;
            }
        }

        private function _compileXml($params = null)
        {
            // Whatever it takes to compile XML
        }
}

Then in your Twig template, you can do something like this...
{% set xml = craft.aiPaginationTest.getXml() %}

{% if false == xml %}
    {# Handle no XML #}
{% endif %}

This might not directly answer your question, but I hope it's enough to get  you back on track.


---

