在智能中创建解析名称的函数的最佳方法是什么?
示例:我有变量{$attachment.filename}
如果文件名是.jpg|.jpeg|.gif|.png -我的结果-(选择在lightbox中打开.否则,在照明箱中没有打开的选项.)
谢谢!
发布于 2014-12-11 18:28:16
在PHP中进行解析,并在Smarty变量中设置一个标志。在模板中,检查标志并显示所需的内容。不要在模板中嵌入逻辑。
如果您确实需要在许多模板中使用此功能,那么实际上,您可以编写一个适合您需要的Smarty插件。
关于智能插件的文档包括一些可以给您灵感的例子。
例如:
// Define the function that implements the plugin
function my_special_img($params, Smarty_Internal_Template $template)
{
$src = $params['src'];
// Based on $src decide here if you want lightbox or not
$lightbox = TRUE; // or FALSE
// Generate the <img> element
// Get information from $params, put default values, do whatever you want
$output = implode(' ', array( // build the string from pieces
'<img',
'src="{$src}"',
'width="{$params['width']}"',
'height="{$params['height']"',
'alt="{$params['alt']}"',
'class="{$params['class']}"',
($lightbox ? 'rel="lightbox"' : ''),
'/>'
));
// Smarty will replace the function call in the template with the value it returns
return $output;
}
// Register the plugin in Smarty
$smarty->registerPlugin('function', 'image', 'my_special_img');在模板中,替换
<img src="filename.jpg" width="40" alt="bla-bla" etc>使用
{image src="filename.jpg" width="40" alt="bla-bla" etc}就这样。在插件的代码中表达您的创造力,但要保持简单,并且只使用$params中提供的值。
https://stackoverflow.com/questions/27429425
复制相似问题