3

I am trying to pass an array on template render like this:


public function actionConfirm()
{
   $variables['confirm'] = array(
      'name'   => 'Silvanus',
      'office' => 'OOTS',
    );
    $this->renderTemplate('confirm', $variables);
}

And output it in layout like this:


{%for variable in variables%}
  {{variable.name}}
  {{variable.office}}

This is just an example, mainly after client pays using paypal I get a payment Object that I want to pass it to the confirmation layout as per explanation above. I have tried it doesn't work.

Anyone has an idea on how to achieve this?

Thanks

Cheers.

1

Yes, you do it with the setRouteVariables mathod on UrlManager, like this:

craft()->urlManager->setRouteVariables(array(
    'variable' => array(
      'name' => 'silvanus',
      'office' => 'OOTS
    )
));

$this->renderTemplate('confirm');

Now you can access your variables like this in your template:

{{ variable.name }}
{{ variable.office }}

Check out the documentation on Controllers, there's an example.

EDIT:
The above method is how I've done it in the past, but I see now that the renderTemplate method also can be used. I think the problem is just how you access it in your templates, I think you should just do:

{{ confirm.name }}
{{ confirm.office }}
2
  • Thank you so much aelvan,the EDITED version of answer is what I was looking for.Cheers mate. Sep 3 '14 at 22:51
  • Can you explain the full code again please? Because I can't get it to work for me. Jun 1 '15 at 18:14

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.