我怎样才能将这样的东西放入 Smarty 模板中?
我正在尝试将 html 和 php 混合的现有网站转换为基于 Smarty 模板的网站。我以前从未使用过 Smarty,所以这对我来说非常困难。我知道您可以像这样分配一个变量:
$smarty->assign('number_of_items_in_cart', $number_of_items_in_cart);
并在 tpl 文件中使用它,如下所示:
{$number_of_items_in_cart}
但是更复杂的事情(例如我在旧站点上的这段代码)又如何呢:
$query = mysql_query(" SELECT * FROM products WHERE id = '$pid' ");
if (mysql_num_rows($query) == 1) {
while ($row = mysql_fetch_array($query)) {
extract($row);
echo "<h2>$name</h2>";
echo "<img src='images/product_images/$image' alt='' width='100' />";
echo $description;
echo '<p>'.money($price).'</p>';
echo "<input type='text' value='1' class='qty-$id' />";
echo "<a href='javascript:void(0)' onClick=\"add_cart('$id')\">Add to Cart</a>";
}
} else {
redirect('404.php');
}
How can I work with this in a Smarty template ,因为输出是在 while 循环内?
I am trying to convert a pre-existing site that had html and php intermingled into a Smarty template based site. I never used Smarty before so this is proving very difficult for me. I get that you can assign a variable like so:
$smarty->assign('number_of_items_in_cart', $number_of_items_in_cart);
and use it in the tpl file like so:
{$number_of_items_in_cart}
but what about more complex things like this block of code that I had on the old site:
$query = mysql_query(" SELECT * FROM products WHERE id = '$pid' ");
if (mysql_num_rows($query) == 1) {
while ($row = mysql_fetch_array($query)) {
extract($row);
echo "<h2>$name</h2>";
echo "<img src='images/product_images/$image' alt='' width='100' />";
echo $description;
echo '<p>'.money($price).'</p>';
echo "<input type='text' value='1' class='qty-$id' />";
echo "<a href='javascript:void(0)' onClick=\"add_cart('$id')\">Add to Cart</a>";
}
} else {
redirect('404.php');
}
How can I work with this in a Smarty template, since the output is within a while loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将其附加到字符串变量中,然后将其传递给 smarty,而不是回显此内容:
现在您可以将其传递给 smarty
我希望这就是您正在寻找的
Instead of echoing this, you can append it in a string variable then pass it to smarty:
Now u can pass it to smarty
I hope this is what you are looking for
您可以使用
foreach
内置函数迭代包含查询结果的数组。您还可以使用
foreachelse
显示备用消息(尽管在本例中您正在重定向)。请参阅 http://www.smarty.net/docsv2/en/language 中的示例 7.9 .function.foreach 。
编辑:还有一个while如果这就是您真正想要的功能。
You can use the
foreach
builtin function to iterate over an array containing your query results.You could also use
foreachelse
to display an alternate message (though in this case you're redirecting).See example 7.9 in http://www.smarty.net/docsv2/en/language.function.foreach .
Edit: there's also a while function if that's what you really want.