你好,我正试图将一个值从php文件发送到tpl文件。
当我在php文件中打印r时,我看到我的值是在数组中定义的,但当我打印tpl文件时,我看不到这个元素。
<img src="{$product.manufacturer_name}" alt="" title="" itemprop="image" />
和我的控制器
$special = Product::getProducts((int)$this->langID , 0,6, 'name', 'DESC', 51);
foreach ($special as $specia)
{
$id_image = Product::getCover($specia['id_product']);
// get Image by id
if (sizeof($id_image) > 0)
{
$image = new Image($id_image['id_image']);
// get image full URL
$image_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath()."-home_default.jpg";
$specia['manufacturer_name']=$image_url;
}
}
$dir = _PS_MODULE_DIR_.'/ptspagebuilder/views/templates/front/widgets/sub/products.tpl';
$tdir = _PS_ALL_THEMES_DIR_ . _THEME_NAME_ . '/modules/ptspagebuilder/views/templates/front/widgets/sub/products.tpl';
if (file_exists($tdir)) {
$dir = $tdir;
}
// get Product cover image (all images is possible retrieve by
// Image::getImages($id_lang, $id_product) or
// $productInstance->getImages($id_lang))
$setting['product_tpl'] = $dir;
$setting['products'] = $special;
$output = array('type' => 'flashsale','data' => $setting);
return $output;
}
如果$special
包含数组,则在脚本中出现错误,请更改foreach循环中变量的副本。尝试更改foreach ($special as $specia)
到foreach ($special as $k=>$specia)
$specia['manufacturer_name']=$image_url;
到$special[$k]['manufacturer_name']=$image_url;
实际上我没有理解你的问题。你说你在php文件中分配了值,但我看不到任何分配的值。为了在php文件中赋值,您必须在smarty变量中赋值,这样您就可以在tpl文件中访问它。为此,你必须编写这样的代码:
在您的控制器中,首先您必须分配值,如:
$this->smarty->assign(array(
'product' => $containing_product,
));
$product包含要在tpl文件中访问的值。
现在,在您的tpl文件中,您可以检查它,如:
{var_dump($product)}
现在,在你的tpl文件中,你可以使用
<img src="{$product}" alt="" title="" itemprop="image" />