Jump to content

Problems 'passing' a $product object from PHP to a smarty template... always lost


YaKs

Recommended Posts

Good morning,

i am newbie working with smarty but web developer for a few years. I need to customize the product list and I decide to play a bit with the templates and php. but, I am having problem to discover how to pass a product object to the smarty template...
concretely I am modifying category.php, I want to make a loop of products and call several times the smarty template to visualize some info from each product. (my goal is to show the combinations for each product). so far I only have a toy example but I cannot make it work! could someone tell me where is my syntax mistake?

category.php


foreach($cat_products as $key => $product)

{

echo "Before template " . $cat_products[$key]['id_product']."

";

$smarty->assign('product', $cat_products[$key]);

$smarty->display(_PS_THEME_DIR_.'product2.tpl');

}



product2.tpl
{include file=$tpl_dir./errors.tpl}


{$product->name|escape:'htmlall':'UTF-8'}





nothing is shown from the template, only the id echoed from PHP file. I also tried to create a new Product object inside the loop but it doesn't seem to work either...

please could someone point out where is my big mistake??

thanks a lot in advance.
jose
Link to comment
Share on other sites

Hi,

well, i didnt manage to make it work even using more directives I found in Smarty website.
I also tried $smarty->assign(array('product' => $product)) and I directly didnt get nothing at all.. I guess was an error 500 but I couldnt see it.

so I think no complex structures are allowed to use in smarty templates. I tried to assign simple strings and that seems to work. Also discovered that the object is converted to a associated arrat inside the loop so the access to the properties is different. finally looks like following...

foreach($cat_products as $key => $product)
{
$smarty->assign('name',$product['name']);
$smarty->display(_PS_THEME_DIR_.'product2.tpl');
}

and the template just

{$name|escape:'htmlall':'UTF-8'}



but this is very annoying as product object has a lot of properties ... if someone knows how to pass the whole object please give me a shout...

regards,
jose
Link to comment
Share on other sites

I don't understand why you are having problems. You can just pass the whole $product object in with the following:

$smarty->assign('product', $product);



Then you can use $product->name to get the name. For an array of products called $products, you use:

{foreach from=$products item=$product}
  {$product.name}
{/foreach}

  • Confused 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...