“Smarty基础知识”
2.5.x:Smarty 3 4.0.x:Smarty 4
:::注意[Smarty版本由XOOPS发布]
| XOOPS版本 | Smarty版本 | 主要差异 |
|---|---|---|
| 11.2.5 | Smarty3.x | {php}允许区块(但不鼓励) |
| 2.7.0+ | Smarty3.x/4.x | 准备 Smarty 4 兼容性 |
| 4.0 | Smarty 4.x | {php} 区块被删除,更严格的语法 |
有关迁移指南,请参阅Smarty-4-Migration。 :::
Smarty是PHP的模板引擎,允许开发人员将表示(HTML/CSS)与应用程序逻辑分开。 XOOPS使用Smarty来满足其所有模板需求,从而实现PHP代码和HTML输出之间的清晰分离。
- 主题-Development - 创建XOOPS主题
- 模板-Variables - 模板中的可用变量
- Smarty-4-Migration - 从Smarty 3 升级到 4
Smarty 是什么?
Section titled “Smarty 是什么?”Smarty规定:
- 关注点分离:将 HTML 保留在模板中,将 PHP 逻辑保留在类中
- 模板继承:从简单的区块构建复杂的布局
- 缓存:通过编译模板提高性能
- 修改器:使用内置-in或自定义函数转换输出
- 安全:控制 PHP 函数模板可以访问的内容
XOOPS Smarty 配置
Section titled “XOOPS Smarty 配置”XOOPS 使用自定义分隔符配置 Smarty:
Default Smarty: { and }XOOPS Smarty: <{ and }>这可以防止与模板中的 JavaScript 代码发生冲突。
变量从 PHP 传递到模板:
// In PHP$GLOBALS['xoopsTpl']->assign('title', 'My Page Title');$GLOBALS['xoopsTpl']->assign('count', 42);{* In template *}<h1><{$title}></h1><p>Total items: <{$count}></p>// PHP$item = [ 'id' => 1, 'title' => 'Article Title', 'author' => 'John Doe'];$GLOBALS['xoopsTpl']->assign('item', $item);{* Template *}<h2><{$item.title}></h2><p>By: <{$item.author}></p>// PHP$GLOBALS['xoopsTpl']->assign('user', $xoopsUser);{* Template *}<p>Welcome, <{$user->getVar('uname')}>!</p>Smarty 中的注释不会呈现给 HTML:
{* This is a comment - it will not appear in the HTML output *}
{* Multi-line comments are also supported*}If/Else声明
Section titled “If/Else声明”<{if $user_logged_in}> <p>Welcome back!</p><{elseif $is_guest}> <p>Hello, Guest!</p><{else}> <p>Please log in.</p><{/if}>{* Equality *}<{if $status == 'published'}>Published<{/if}><{if $status eq 'published'}>Published<{/if}>
{* Inequality *}<{if $count != 0}>Has items<{/if}><{if $count neq 0}>Has items<{/if}>
{* Greater/Less than *}<{if $count > 10}>Many items<{/if}><{if $count gt 10}>Many items<{/if}><{if $count < 5}>Few items<{/if}><{if $count lt 5}>Few items<{/if}>
{* Greater/Less than or equal *}<{if $count >= 10}>Ten or more<{/if}><{if $count gte 10}>Ten or more<{/if}><{if $count <= 5}>Five or less<{/if}><{if $count lte 5}>Five or less<{/if}>
{* Logical operators *}<{if $logged_in && $is_admin}>Admin Panel<{/if}><{if $logged_in and $is_admin}>Admin Panel<{/if}><{if $option1 || $option2}>One option selected<{/if}><{if $option1 or $option2}>One option selected<{/if}><{if !$is_banned}>Access granted<{/if}><{if not $is_banned}>Access granted<{/if}>检查 Empty/Isset
Section titled “检查 Empty/Isset”{* Check if variable exists and has value *}<{if $title}> <h1><{$title}></h1><{/if}>
{* Check if array is not empty *}<{if $items|@count > 0}> <ul> <{foreach $items as $item}> <li><{$item.name}></li> <{/foreach}> </ul><{/if}>
{* Using isset *}<{if isset($description)}> <p><{$description}></p><{/if}>Foreach 循环
Section titled “Foreach 循环”{* Basic foreach *}<ul><{foreach $items as $item}> <li><{$item.name}></li><{/foreach}></ul>
{* With key *}<{foreach $options as $key => $value}> <option value="<{$key}>"><{$value}></option><{/foreach}>
{* With @index, @first, @last *}<{foreach $items as $item}> <{if $item@first}><ul><{/if}> <li class="item-<{$item@index}>"><{$item.name}></li> <{if $item@last}></ul><{/if}><{/foreach}>
{* Alternate row colors *}<{foreach $rows as $row}> <tr class="<{if $row@iteration is odd}>odd<{else}>even<{/if}>"> <td><{$row.name}></td> </tr><{/foreach}>
{* Foreachelse for empty arrays *}<{foreach $items as $item}> <li><{$item.name}></li><{foreachelse}> <li>No items found.</li><{/foreach}>For 循环
Section titled “For 循环”<{for $i=1 to 10}> <p>Item <{$i}></p><{/for}>
<{for $i=10 to 1 step -1}> <p>Countdown: <{$i}></p><{/for}>While 循环
Section titled “While 循环”<{while $count > 0}> <p><{$count}></p> <{$count = $count - 1}><{/while}>修饰符转换变量输出:
字符串修饰符
Section titled “字符串修饰符”{* HTML escape (always use for user input!) *}<{$title|escape}><{$title|escape:'html'}>
{* URL encoding *}<{$url|escape:'url'}>
{* Uppercase/Lowercase *}<{$name|upper}><{$name|lower}><{$name|capitalize}>
{* Truncate text *}<{$content|truncate:100:'...'}>
{* Strip HTML tags *}<{$html|strip_tags}>
{* Replace *}<{$text|replace:'old':'new'}>
{* Word wrap *}<{$text|wordwrap:80:"\n"}>
{* Default value *}<{$optional_var|default:'No value'}>{* Number formatting *}<{$price|string_format:"%.2f"}><{$count|number_format}>
{* Date formatting *}<{$timestamp|date_format:"%B %e, %Y"}><{$timestamp|date_format:"%Y-%m-%d %H:%M"}>{* Count items *}<{$items|@count}> items
{* Join array *}<{$tags|@implode:', '}>
{* JSON encode *}<{$data|@json_encode}><{$content|strip_tags|truncate:200:'...'|escape}>包括其他模板
Section titled “包括其他模板”{* Include a template file *}<{include file="db:mymodule_header.tpl"}>
{* Include with variables *}<{include file="db:mymodule_item.tpl" item=$currentItem}>
{* Include with assigned variables *}<{include file="db:sidebar.tpl" assign="sidebar_content"}><div class="sidebar"><{$sidebar_content}></div>插入动态内容
Section titled “插入动态内容”{* Insert calls a PHP function for dynamic content *}<{insert name="getBanner"}>在模板中分配变量
Section titled “在模板中分配变量”{* Simple assignment *}<{assign var="page_title" value="Welcome"}><{$page_title = "Welcome"}>
{* Assignment from expression *}<{assign var="full_name" value="`$first_name` `$last_name`"}>
{* Capture block content *}<{capture name="sidebar"}> <h3>Sidebar</h3> <ul> <li>Link 1</li> <li>Link 2</li> </ul><{/capture}><div class="sidebar"><{$smarty.capture.sidebar}></div>内置-inSmarty变量
Section titled “内置-inSmarty变量”$smarty 变量
Section titled “$smarty 变量”{* Current timestamp *}<{$smarty.now|date_format:"%Y-%m-%d"}>
{* Request variables *}<{$smarty.get.page}><{$smarty.post.username}><{$smarty.request.id}><{$smarty.cookies.session_id}><{$smarty.server.HTTP_HOST}>
{* Constants *}<{$smarty.const.XOOPS_URL}>
{* Configuration variables *}<{$smarty.config.var_name}>
{* Template info *}<{$smarty.template}><{$smarty.current_dir}>
{* Smarty version *}<{$smarty.version}>
{* Section/Foreach properties *}<{$smarty.foreach.items.index}><{$smarty.foreach.items.iteration}><{$smarty.foreach.items.first}><{$smarty.foreach.items.last}>对于带大括号的 JavaScript:
<{literal}><script> var config = { url: 'https://example.com', count: 10 }; if (config.count > 5) { console.log('Many items'); }</script><{/literal}>或者在 JavaScript 中使用 Smarty 变量:
<script>var moduleUrl = '<{$xoops_url}>/modules/mymodule';var items = <{$items_json}>;</script>XOOPS提供自定义Smarty功能:
{* XOOPS Image URL *}<img src="<{xoImgUrl}>images/logo.png" alt="Logo">
{* XOOPS Module URL *}<a href="<{xoModuleUrl}>">Module Home</a>
{* App URL *}<a href="<{xoAppUrl 'item.php'}>?id=<{$item.id}>">View Item</a>始终转义输出
Section titled “始终转义输出”{* For user-generated content, always escape *}<p><{$user_comment|escape}></p>
{* For HTML content, use appropriate method *}<div><{$content}></div> {* Only if content is pre-sanitized *}使用有意义的变量名称
Section titled “使用有意义的变量名称”// Good$GLOBALS['xoopsTpl']->assign('article_title', $title);$GLOBALS['xoopsTpl']->assign('article_items', $items);
// Avoid$GLOBALS['xoopsTpl']->assign('t', $title);$GLOBALS['xoopsTpl']->assign('arr', $items);保持逻辑最少
Section titled “保持逻辑最少”模板应侧重于演示。将复杂逻辑移至PHP:
{* Avoid complex logic in templates *}{* Bad *}<{if $user && $user->getVar('level') > 5 && $user->getVar('status') == 'active' && $permissions|in_array:'edit'}>
{* Good - calculate in PHP and pass a simple flag *}<{if $can_edit}>使用模板继承
Section titled “使用模板继承”为了获得一致的布局,请使用模板继承(请参阅主题-Development)。
{* Show all assigned variables *}<{debug}>{* Debug specific variable *}<pre><{$variable|@print_r}></pre><pre><{$variable|@var_export}></pre>常见XOOPS模板模式
Section titled “常见XOOPS模板模式”模区块模板结构
Section titled “模区块模板结构”{* Module header *}<div class="mymodule"> <h2><{$module_name}></h2>
{* Breadcrumb *} <{if $breadcrumb}> <nav class="breadcrumb"> <{foreach $breadcrumb as $crumb}> <{if $crumb@last}> <span><{$crumb.title}></span> <{else}> <a href="<{$crumb.link}>"><{$crumb.title}></a> » <{/if}> <{/foreach}> </nav> <{/if}>
{* Content *} <div class="content"> <{$content}> </div></div><{if $page_nav}><div class="pagination"> <{$page_nav}></div><{/if}><{if $form}><div class="form-container"> <{$form}></div><{/if}>#smarty #模板#XOOPS #前端#模板-engine