我怎样才能将这样的东西放入 Smarty 模板中?

发布于 2024-11-06 01:28:33 字数 1010 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-11-13 01:28:33

您可以将其附加到字符串变量中,然后将其传递给 smarty,而不是回显此内容:

$string = "";
while ($row = mysql_fetch_array($query)) {
        extract($row);
        $string .= "<h2>$name</h2>";
        $string .=  "<img src='images/product_images/$image' alt='' width='100' />";
        $string .=  $description;
        $string .=  '<p>'.money($price).'</p>';
        $string .=  "<input type='text' value='1' class='qty-$id' />";
        $string .=  "<a href='javascript:void(0)' onClick=\"add_cart('$id')\">Add to Cart</a>";
    }

现在您可以将其传递给 smarty

$smarty->assign('place_holder', $string);

我希望这就是您正在寻找的

Instead of echoing this, you can append it in a string variable then pass it to smarty:

$string = "";
while ($row = mysql_fetch_array($query)) {
        extract($row);
        $string .= "<h2>$name</h2>";
        $string .=  "<img src='images/product_images/$image' alt='' width='100' />";
        $string .=  $description;
        $string .=  '<p>'.money($price).'</p>';
        $string .=  "<input type='text' value='1' class='qty-$id' />";
        $string .=  "<a href='javascript:void(0)' onClick=\"add_cart('$id')\">Add to Cart</a>";
    }

Now u can pass it to smarty

$smarty->assign('place_holder', $string);

I hope this is what you are looking for

和我恋爱吧 2024-11-13 01:28:33

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文