首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >(智能)函数preg_match或其他最佳方法

(智能)函数preg_match或其他最佳方法
EN

Stack Overflow用户
提问于 2014-12-11 18:01:25
回答 1查看 2.1K关注 0票数 0

在智能中创建解析名称的函数的最佳方法是什么?

示例:我有变量{$attachment.filename}

如果文件名是.jpg|.jpeg|.gif|.png -我的结果-(选择在lightbox中打开.否则,在照明箱中没有打开的选项.)

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-11 18:28:16

在PHP中进行解析,并在Smarty变量中设置一个标志。在模板中,检查标志并显示所需的内容。不要在模板中嵌入逻辑。

如果您确实需要在许多模板中使用此功能,那么实际上,您可以编写一个适合您需要的Smarty插件。

关于智能插件的文档包括一些可以给您灵感的例子。

例如:

代码语言:javascript
运行
复制
// 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');

在模板中,替换

代码语言:javascript
运行
复制
<img src="filename.jpg" width="40" alt="bla-bla" etc>

使用

代码语言:javascript
运行
复制
{image src="filename.jpg" width="40" alt="bla-bla" etc}

就这样。在插件的代码中表达您的创造力,但要保持简单,并且只使用$params中提供的值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27429425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档