将变量传递给 Smarty 视图文件
Passing Variables to Smarty view file
您好,我正在尝试将 php 文件中的值发送到 tpl 文件。
当我在 php 文件中 print_r 时,我看到我的值是在数组中定义的,但是当我 print_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 循环中更改 vars 的副本。尝试改变
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" />
您好,我正在尝试将 php 文件中的值发送到 tpl 文件。
当我在 php 文件中 print_r 时,我看到我的值是在数组中定义的,但是当我 print_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 循环中更改 vars 的副本。尝试改变
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" />