首页/开发手册/条件判断与格式化标签

开发手册

SoeayCMS 安装、标签使用与二次开发完整指南

条件判断与格式化标签

条件判断标签用于在模板中实现逻辑分支。

基本语法

{soeay:if('{soeay:content.ico}'!='')}
    <img src="{soeay:content.ico}">
{else}
    <img src="/static/nopic.png">
{/soeay:if}

支持的运算符

运算符说明
==等于
!=不等于
>大于
<小于
>=大于等于
<=小于等于
&&逻辑与
||逻辑或

多条件组合示例

{soeay:if('{soeay:content.ico}'!='' && '{soeay:content.visits}'>'100')}
    <p>热门且有图</p>
{/soeay:if}

格式化属性

格式化属性可直接写在标签上,对输出的字段做二次处理。块标签内部变量(如 [soeay:content])与单标签(如 {soeay:content.content}、{soeay:prev_content.title})均支持,多个属性可叠加使用。

属性说明示例
len=*按字符数截取[soeay:title len=20]
lencn=*按中文字数截取[soeay:title lencn=10]
substr=x,y从 x 位起截取 y 个字符[soeay:title substr=0,10]
drophtml=1去除 HTML 标签(输出纯文本)[soeay:content drophtml=1]
{soeay:content.content drophtml=1}
dropblank=1去除空白字符(换行符、制表符、空格等)[soeay:content dropblank=1]
decode=1解码 HTML 实体(如 < 还原为 <){soeay:content.title decode=1}
decode=0不解码,保持原样(包含已解码源码)。此标签一般不使用{soeay:content.title decode=0}
decimal=*保留指定小数位数{soeay:content.visits decimal=2}
unit=*容量单位换算,可为 b / kb / mb / gb / tb / pb / eb,或 unit=auto 自动选择{soeay:content.filesize unit=mb decimal=2}
operate=**数字运算,支持 + - * / %。如访问量加 1000、序号求余[soeay:visits operate=+1000]
[soeay:i operate=%2]
mark=关键词将关键词标红(输出 关键词,大小写不敏感)[soeay:title mark=手机]
mark=1自动使用当前搜索关键词标红(搜索结果页专用)[soeay:title mark=1]
{soeay:search_keyword} 所在页可用

格式化属性示例

列表摘要去 HTML 并限制长度:
[soeay:content drophtml=1 len=80]

单标签同样支持(内容详情页正文去 HTML):
{soeay:content.content drophtml=1}

访问量格式化并加 1000:
{soeay:content.visits operate=+1000 decimal=0}

文件大小自动单位:
{soeay:content.filesize unit=auto decimal=2}

搜索结果标红(自动取搜索词):
[soeay:title mark=1]

lfield 限制查询字段(性能优化)

lfield="字段1,字段2" 用于列表、指定内容等查询标签,限制只查询指定字段,在大数据量时可显著提升性能。字段需在白名单内(id、scode、title、subtitle、filename、ico、date、visits、istop、isrecommend、isheadline、keywords、description、content、author、source、acode、create_time、update_time)。

{soeay:list scode=1 lfield="title,content"}
    [soeay:title]
    [soeay:content drophtml=1 len=100]
{/soeay:list}

使用场景

条件判断标签({soeay:if}...{else}...{/soeay:if})用于在模板中根据字段值动态输出不同内容,如"有缩略图显示图片、否则显示默认图""VIP 内容仅对登录用户可见"等。

写法要点

  • 判断的变量需用单引号包裹,如 {soeay:if('{soeay:content.ico}'!='')},避免空值导致语法错误。
  • 支持嵌套,但不宜过深,否则影响可读性。
  • 运算符与 PHP 一致,字符串比较用 == / !=,数值比较可直接用 > / <。
  • 多条件组合时建议用括号明确优先级,例如 (a == '1' && b != '')。
  • 格式化属性中 drophtml、dropblank、decimal、unit、operate、mark、decode 同时支持块标签与单标签写法。