ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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);
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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