1

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

<?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.
            }
}
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.