我如何在 Smarty 中使用一组 PHP 函数
How can I use set of PHP functions in Smarty
我真的是 smarty 的新手。我想知道如何在 smarty 中使用一组 PHP 函数。我知道有些功能可以直接使用
例如:{$my_string|strip_tags}
我正在使用 PDO 和 smarty。请看下面的代码,我在哪里得到我的帖子
$stmnt = $conn->prepare("SELECT post_id, post_title FROM posts ORDER BY post_id DESC");
$stmnt ->execute();
$row = $stmnt ->fetchAll();
$my_template = new Smarty;
$my_template->debugging = false;
$my_template->caching = true;
$my_template->cache_lifetime = 120;
$my_template->setTemplateDir('./templates/’');
$my_template->assign("row", $row);
$my_template->display('posts.tpl');
//in my smartyposts.tpl
{foreach $row as $r}
//display posts here
{/foreach}
我想使用一些 php 函数从 post_title 创建 url。所以通常我在 php
中做什么
<?php
foreach($row as $r){
$post_title = $r[‘post_title’];
$create_link = preg_replace("![^a-z0-9]+!i", "-", $post_title);
$create_link = urlencode($create_link);
$create_link = strtolower($create_link);
?>
<a href="posts/<?php echo $create_link;?>”><?php echo $post_title ;?></a>
<?php } ?>
如何使用 smarty 实现相同的输出?我确实到处搜索,但找不到任何答案。感谢您的宝贵时间。
创建 modifier:
test.php
<?php
require_once('path/to/libs/Smarty.class.php');
function smarty_modifier_my_link($title) {
$link = preg_replace("![^a-z0-9]+!i", "-", $title);
$link = urlencode($link);
return strtolower($link);
}
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates/');
$smarty->setCompileDir(__DIR__ . '/templates_c/');
$smarty->setConfigDir(__DIR__ . '/configs/');
$smarty->setCacheDir(__DIR__ . '/cache/');
$smarty->registerPlugin('modifier', 'my_link', 'smarty_modifier_my_link');
$smarty->assign('posts', [
['post_title' => 'Some Title 1'],
['post_title' => 'Some Title 2'],
]);
$smarty->display('index.tpl');
templates/index.tpl
{foreach $posts as $p}
<a href="/posts/{$p.post_title|my_link}">{$p.post_title|htmlspecialchars}</a>
{/foreach}
输出
<a href="/posts/some-title-1">Some Title 1</a>
<a href="/posts/some-title-2">Some Title 2</a>
我真的是 smarty 的新手。我想知道如何在 smarty 中使用一组 PHP 函数。我知道有些功能可以直接使用
例如:{$my_string|strip_tags}
我正在使用 PDO 和 smarty。请看下面的代码,我在哪里得到我的帖子
$stmnt = $conn->prepare("SELECT post_id, post_title FROM posts ORDER BY post_id DESC");
$stmnt ->execute();
$row = $stmnt ->fetchAll();
$my_template = new Smarty;
$my_template->debugging = false;
$my_template->caching = true;
$my_template->cache_lifetime = 120;
$my_template->setTemplateDir('./templates/’');
$my_template->assign("row", $row);
$my_template->display('posts.tpl');
//in my smartyposts.tpl
{foreach $row as $r}
//display posts here
{/foreach}
我想使用一些 php 函数从 post_title 创建 url。所以通常我在 php
中做什么 <?php
foreach($row as $r){
$post_title = $r[‘post_title’];
$create_link = preg_replace("![^a-z0-9]+!i", "-", $post_title);
$create_link = urlencode($create_link);
$create_link = strtolower($create_link);
?>
<a href="posts/<?php echo $create_link;?>”><?php echo $post_title ;?></a>
<?php } ?>
如何使用 smarty 实现相同的输出?我确实到处搜索,但找不到任何答案。感谢您的宝贵时间。
创建 modifier:
test.php
<?php
require_once('path/to/libs/Smarty.class.php');
function smarty_modifier_my_link($title) {
$link = preg_replace("![^a-z0-9]+!i", "-", $title);
$link = urlencode($link);
return strtolower($link);
}
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates/');
$smarty->setCompileDir(__DIR__ . '/templates_c/');
$smarty->setConfigDir(__DIR__ . '/configs/');
$smarty->setCacheDir(__DIR__ . '/cache/');
$smarty->registerPlugin('modifier', 'my_link', 'smarty_modifier_my_link');
$smarty->assign('posts', [
['post_title' => 'Some Title 1'],
['post_title' => 'Some Title 2'],
]);
$smarty->display('index.tpl');
templates/index.tpl
{foreach $posts as $p}
<a href="/posts/{$p.post_title|my_link}">{$p.post_title|htmlspecialchars}</a>
{/foreach}
输出
<a href="/posts/some-title-1">Some Title 1</a>
<a href="/posts/some-title-2">Some Title 2</a>