ClassCMS

输入框

某些后台输入框在存储数据时,会将数据格式化后存储,前台输出时,需要将内容提前格式化.

如日期时间输入框,数据库中存储为unix时间戳格式,需要先使用datetime函数转化为2020-06-06 06:06:00 的格式

栏目变量输出

{date(Y-m-d H:i:s,$.riqi)}

文章循环时输出文章字段

{loop a() as $article}
    {date(Y-m-d H:i:s,$article.riqi)}
{/loop}

文章页输出文章字段

{date(Y-m-d H:i:s,$riqi)}

常见特殊输入框的内容输出(此处都为内容页字段形式$xxx,如果为栏目变量则格式为$.xxx,如果在文章列表中则格式为$article.xxx):

开关

打开时数据库中存储值为1,关闭是为0

{if $kaiguan}
    开了
{else}
    关了
{/if}

多图/文件输出

当图片上传/文件上传 输入框配置开启了 多图/多文件时,内容保存格式为 /upload/xxx/1.jpg;/upload/xxx/2.jpg

需要先使用explode函数将内容分割,再使用loop语法输出

{$files=explode(;,$duotu)|array_filter()}
{loop $files as $file}
    {$file}<br>
{/loop}

图标

详见:https://www.layui.com/doc/element/icon.html

<i class="layui-icon {$tubiao}"></i>

多项文本框(tags)

当列数为1时,存储格式为: 第一项;第二项;第三项

{$tags=explode(;,$duoxiang)}
{loop $tags as $tag}
    {if $tag}{$tag}<br>{/if}
{/loop}

当列数多余1时,存储格式为:第一项列1|第一项列2;第二项列1|第二项列2

{$tags=explode(;,$duoxiang)}
{loop $tags as $tag}
    {$columns=explode(\|,$tag)}
    第一项:{$columns.0}
    第二项:{$columns.1}
    循环输出:
    {loop $columns as $column}
        {$column}/
    {/loop}<br>
{/loop}

单选框/列表框/穿梭框

当保存格式选择为"值" 时,数据库中仅保存了序号,前台输出时需要手动输出对应序号的标题

{if $danxuan==1}
    选择了第一项
{elseif  $danxuan==2}
    选择了第二项
{/if}

通过自定义方法查询对应的标题

//方法代码(方法代码需要保存在应用主文件中):
function valueTitle($value,$hash='',$kind='column',$modulehash='article',$classhash=''){
    if(empty($classhash)) {$classhash=I();}
    if(empty($value)){
        return false;
    }
    if(!$form=C('cms:form:get',$hash,$kind,$modulehash,$classhash)){
        return false;
    }
    $info=C('cms:form:build',$form['id']);
    if(!isset($info['values'])){
        return false;
    }
    if(isset($info['savetype']) && $info['savetype']==0){
        return $value;
    }
    $values=explode("\n",$info['values']);
    foreach ($values as $key => $thisvalue) {
        $thisvalue=explode(':',$thisvalue);
        if($thisvalue[0]==$value && isset($thisvalue[1])){
            return $thisvalue[1];
        }
    }
    return false;
}

//调用方法:

文章循环时调用文章字段(xxx)对应的标题:

{this:valueTitle($article.xxx,'xxx','','模型标识')}

文章页中调用文章字段(xxx)对应的标题:
{this:valueTitle($xxx,'xxx','','模型标识')}

查询栏目变量xxx的值对应的标题
{this:valueTitle($.xxx,'xxx','var','模型标识')}

文章选择输入框

保存类型为id,则通过id查询到对应的文章,建议先通过empty函数判断当前文章字段是否已选择.

{$a.cid=栏目id} {//改成输入框配置中文章来源栏目id}
{$a.where.id=$wenzhang} {//查询条件为id=选择的id}
{$article=cms:article:getOne($a)}
{if $article}
    选择的文章:{$article.title} 文章网址:{$article.link}
{else}
    文章不存在
{/if}

保存类型为cid+id,数据库中的格式为cid:id

{$set=explode(:,$wenzhang)}
{$a.cid=$set.0}
{$a.where.id=$set.1}
{$article=cms:article:getOne($a)}
{if $article}
    选择的文章:{$article.title} 文章网址:{$article.link}
{else}
    文章不存在
{/if}

启用多选后,保存格式为 1;2;3或1:1;1:2;1:3,先使用explode函数分割 id格式:

{$ids=explode(;,$wenzhang)}
{loop $ids as $thisid}
    {if !empty($thisid)}
        {$a.cid=栏目id} {//改成输入框配置中文章来源栏目id}
        {$a.where.id=$thisid}
        {$article=cms:article:getOne($a)}
        {if $article}
            选择的文章:{$article.title} 文章网址:{$article.link}<br>
        {else}
            文章不存在<br>
        {/if}
    {/if}
{/loop}

cid:id格式:

{$sets=explode(;,$wenzhang)}
{loop $sets as $thisset}
    {if !empty($thisset)}
        {$set=explode(:,$thisset)}
        {$a.cid=$set.0}
        {$a.where.id=$set.1}
        {$article=cms:article:getOne($a)}
        {if $article}
            选择的文章:{$article.title} 文章网址:{$article.link}<br>
        {else}
            文章不存在<br>
        {/if}
    {/if}
{/loop}

文章单选框/列表框

如保存类型选择文章id,则需要查找对应的文章

{$a.cid=栏目id} {//改成输入框配置中文章来源栏目id}
{$a.where.id=$danxuan} 
{$article=cms:article:getOne($a)}
{if $article}
    选择的文章:{$article.title} 文章网址:{$article.link}
{else}
    文章不存在
{/if}

文章多选框/穿梭框

如保存类型选择文章id,数据库中保存的类型为:1;2;3,需要使用explode分割id,并查找对应的文章

{$ids=explode(;,$duoxuan)}
{loop $ids as $thisid}
    {if !empty($thisid)}
        {$a.cid=栏目id} {//改成输入框配置中文章来源栏目id}
        {$a.where.id=$thisid} 
        {$article=cms:article:getOne($a)}
        {if $article}
            选择的文章:{$article.title} 文章网址:{$article.link}<br>
        {else}
            文章不存在<br>
        {/if}
    {/if}
{/loop}