2

SUMMARY: I need to access an element in an array that is passed to a template from a module and test for it's existence.

I have a function in a module that looks like this:

function subscriptions(){
  $subscriptions = array('sub1'=>array(...unimportant data...),
                         'sub4'=>array(...unimportant data...)
                        );
  $data['subscriptions'] = $subscriptions;
  $output = $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $data);
  return $output;
}

and a template that looks like

{exp:my_module:subscriptions} {if subscriptions:sub4}User is subscribed to sub4{/if} {/exp:my_module:subscriptions}

but that doesn't seem to work. I want to display certain things based upon what subscriptions a user has. This is not just to iterate through and print out subscriptions.

I also tried

<?php if($subscriptions['sub4']){ echo 'User is subcribed to sub4' } ?>

and it says subscriptions isn't set.

2 Answers 2

2

If you want your variables to be in the subscriptions:sub4 format, you should try this:

foreach ($subscriptions as $key => $value) {
  $data['subscriptions:'.$key] = $value;
}
return $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $data);
0

Rob's got it - but just to elaborate, the code you're using in your question would require you to use template code which looks like this:

{exp:my_module:subscriptions}
    {subscriptions}
        {if sub4}User is subscribed to sub4{/if}
    {/subscriptions}
{/exp:my_module:subscriptions}

The "colon" shortcut which EE uses in Grid and Relationship fields is not a default feature of the parse_variables() methods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.