0

I want to use a loop in smarty to read an object value which i will be passing from controller later on.

{foreach from=$foo->bars item=bar}
    {$bar->product->name}
    {$bar->code}
{/foreach}

To initialize this, i wrote the following code in smarty file:

{php}
    $bar1->product->name =  "prod1";
    $bar1->code = "BC5E";

    $bar2->product->name =  "prod2";
    $bar2->occasionName = "XW9D";

    $this->assign($foo->bars, array($bar1,$bar2);
{/php}

But this gives me an error, is this the right way to create an object in Smarty ?

2 Answers 2

1
$this->assign($foo->bars, array($bar1,$bar2));

You forgot to close last ).

Sign up to request clarification or add additional context in comments.

1 Comment

still, $foo->bars is empty
0

Can you please do all the execution with the object on the php page and then you can assign the final array on to the smarty and use easily on the .tpl page this is the best way.

Try not to process data on the .tpl page process all the data on php page and assign it to .tpl and then just show it.

Smarty is a template engine and is designed for view(presentation) mainly.

Write the foreach loop on .php create array output and assign to tpl.

1 Comment

as i say .. this is for testing and i will be getting data from controller later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.