We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

pass array value to a tag?

Development and Programming

Cheaphotels's avatar
Cheaphotels
12 posts
15 years ago
Cheaphotels's avatar Cheaphotels

is it possible to pass an array variable as a parameter to a tag?

like:

{exp:mytag:subtag myparameter="<?php echo $data_array; ?>"}

Moved to Development and Programming by Moderator

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
15 years ago
Sue Crocker's avatar Sue Crocker

Hi, Cheaphotels. Since it appears you’re wanting to write your own add-on, I’ve moved this topic to the Development forum for additional community support.

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

No, you will need to serialize it some way. Try something like this.

{exp:mytag:subtag myparameter="<?php echo serialize($data_array); ?>"} 

    public function subtag() 
    {    
            $myparameter = (($myparameter = $this->EE->TMPL->fetch_param('myparameter', FALSE)) !== FALSE) ? @unserialize($myparameter) : FALSE;

            if ($myparameter !== FALSE AND is_array($myparameter)) 
            {
                // should be an array
            }
    }
       
Cheaphotels's avatar
Cheaphotels
12 posts
15 years ago
Cheaphotels's avatar Cheaphotels

tried serializing it. the array is broken. im making a plugin that preferably uses an array as input to process data.

code on template:

<?php
    $myarray = array(
            'rere' => 'dasdad',
            'lala' => 'dsdsd'
        );
?>
{exp:mytag:mymethod data_array="<?php echo serialize($myarray); ?>"}

echo echoed, myarray returns

a🔢{s:4:"rere";s:6:"dasdad";s:4:"lala";s:5:"dsdsd";}

code inside the plugin:

$data_array = $this->EE->TMPL->fetch_param('data_array');
print_r ($data_array);

returns a🔢{s:4:

tried using stripslashes. same thing, the array is broken. any ideas?

       
the3mus1can's avatar
the3mus1can
426 posts
15 years ago
the3mus1can's avatar the3mus1can

Here is another untested attemp:

{exp:mytag:subtag myparameter="<?php echo http_build_query($data_array); ?>"} 

        public function subtag() 
        {    
               $myparameter = $this->EE->TMPL->fetch_param('myparameter', FALSE);
                    parse_str($myparameter, $myarray);

                if (is_array($myarray)) 
                {
                    // should be an array
                }
        }

If that does not work you can always take the approach of getting the data in by another means besides a parameter. Take a look at http://ellislab.com/expressionengine/user-guide/development/usage/session.html#cache

       
Cheaphotels's avatar
Cheaphotels
12 posts
15 years ago
Cheaphotels's avatar Cheaphotels
Here is another untested attemp:
{exp:mytag:subtag myparameter="<?php echo http_build_query($data_array); ?>"} 

        public function subtag() 
        {    
               $myparameter = $this->EE->TMPL->fetch_param('myparameter', FALSE);
                    parse_str($myparameter, $myarray);

                if (is_array($myarray)) 
                {
                    // should be an array
                }
        }
If that does not work you can always take the approach of getting the data in by another means besides a parameter. Take a look at http://ellislab.com/expressionengine/user-guide/development/usage/session.html#cache

thanks for the help, i figured it out

here goes

{exp:mytag:subtag  data_array="<?php echo urlencode(serialize($data_array)); ?>" }

to access it

$data_array = $this->EE->TMPL->fetch_param('data_array');
$data_array = unserialize(urldecode($data_array));

it seems that the problem is on the double quotes and curly brackets. CHEERS!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.