织梦CMS常用的几种字段判断输出实例

前端之家收集整理的这篇文章主要介绍了织梦CMS常用的几种字段判断输出实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',Verdana,sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 我们在使用织梦CMS制作网站时,对于某个字段,无论是默认字段还是自定义字段,偶尔会使用一些判断语句来实现我们的需求。下边列出了几种常见的需求。以及字段的判断输出实例。 

Box-sizing:border-Box;outline:0px;font-family:'Microsoft YaHei',sans-serif;line-height:27px;color:#666666;border-radius:3px;border-left-width:3px;border-left-style:solid;border-left-color:#0EBAA6;background-color:rgba(102,128,153,0.0470588);"> 第一种:织梦自定义字段,如果没有值的时候要显示指定的默认内容

Highlighter_309678" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace !important;font-size:14px !important;min-height:auto !important;background-image:none !important;background-attachment:initial !important;background-size:initial !important;background-origin:initial !important;background-clip:initial !important;background-position:initial !important;background-repeat:initial !important;">
 PHP= Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;">  

Box-sizing:border-Box;outline:0px;font-family:'Microsoft YaHei',0.0470588);"> 第二种:Dedecms判断简略标题为空时则显示完整标题

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 方法1,适用于内容

Highlighter_130265" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;color:blue !important;background:none !important;">'array'  
 
方法2,适用于列表页 

Highlighter_722170" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;background:none !important;">[field: 

Box-sizing:border-Box;outline:0px;font-family:'Microsoft YaHei',0.0470588);"> 第三种:对某一个字段多重判断

Highlighter_764739" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;color:blue !important;background:none !important;">'risklevel'
 
Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;">
以上意思就是说如果查询得到的风险等级字段的取值是HR,那么将输出HR.png,如果查询得到的风险等级字段的取值是D,那么将输出D.png,如果查询得到的风险等级字段的取值是AA,那么将输出AA.png,这里的HR.png,只是一个字符串,代表图片文件所在的位置,你可以设置成任意的图片路径,上面的代码是一种分支条件输出语句,@me代表的就是输出变量,刚进入标签field:array时,@me代表数据库的一条记录,其数据类型是数组,我们根据其中risklevle的取值来判断,从而将@me设置成不同的值,当退出标签field:array时,@me的值就会被显示在原位置。

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 以上分享结束,如有疑问可以在下边加群留言跟进,编程之家会第一时间回复。 

Box-sizing:border-Box;outline:0px;font-family:'Microsoft YaHei',0.0470588);"> 第四种:判断过程中需要调用别的字段。

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 需要在模板里做一个小判断,如果文章的价格为0时直接输出其下载地址,否则直接输出购买链接,那么这个时候肯定是需要运行runPHP='yes'这个功能了,下面我们来看下代码: 经过搜索发现两个方法基本上可以实现 1、首先把需要调用其它字段的值放在公共变量里,然后再直接调用公共变量的值,看代码

Highlighter_953959" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;background:none !important;">{dede:PHP}Fields[PHP}
PHP}
PHP=
链接为:XXX"
Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;">  

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 2、直接使用name='array'将其它字段的值赋值到数组里,然后使用,看代码

Highlighter_264217" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px 0.3em 0px 0px !important;border:0px !important;outline:0px !important;text-align:right !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:2.7em !important;min-height:auto !important;display:block !important;background:none !important;">01    下载:
  

Box-sizing:border-Box;outline:0px;font-family:'Microsoft YaHei',0.0470588);"> 第五种 案例-标题列表判断是否为当日新发布以及是否为头条推荐等原创代码

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 织梦CMS常用的几种字段判断输出实例

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 看到图片就应该很熟悉了,日常做Dedecms模版的时候,首页肯定会有这样或那样的文章标题列表,用{dede:arclist}调用列表出来是件很easy的事情这里就不过多叙述了,不太熟悉的童鞋可以去参考Dedecms官方文档。这里我主要想说的是有点时候我们想在这个文章标题列表上做点小文章,比如取出的标题是当天发布的内容,则在标题后面加上new图标;又比如头条推荐的文章标题后面加上上图里的top图标等等,而单纯透过{dede:arclist}里面的'flag'属性是无法单独判断的,这就需要我们单独做点什么了。既然需要判断,自然而然的会令我们想到用'if',在Dedecms里面如果使用'if'判断的内容还跟文章有关,那不得不提的就是[field:array]这个东西很强大啊,基本上你想要的东西都在这里面,下面来看代码,先来两个热热身: 

Box-sizing:border-Box;outline:0px;color:#4E4E4E;font-family:'Microsoft YaHei',sans-serif;font-size:15px;line-height:24px;background-color:#FFFFFF;"> 1、循环判断{dede:arclist}取出的标题是否为头条

Highlighter_677691" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;background:none !important;">{dede:arclist row=4 orderby=pubdate typeid=2}
PHP=
文章是头条推荐'
文章不是头条推荐'

代码不长,比较好理解,因为这里我们只是要对文章里的'flag'属性做判断,所以如果使用[field:array]虽然也能达到效果,但是会取出很多多余的内容难免影响效率,而直接使用[field:flag]既简洁又方便,其中"@me = 'h'"是判断文章是否属于头条,聪明的童鞋看到这里一定会衍生出很多种判断了,因为flag的值除了'h'外还可以是"头条[h]推荐[c]幻灯[f]特荐[a]滚动[s]加粗[b]图片[p]跳转[j]"。Dedecms模版的童鞋可能对这个@me使用起来比较晕,不要紧。Dedecms里面虽然可以执行PHP语句,但是不支持用'echo'进行输出,所以'@me'最简单的一个功能就是代替'echo'进行输出。比如本段代码中的"@me='这篇文章是头条推荐';",如果用PHP写法就应该是"echo '这篇文章是头条推荐';"这样看是否直观明白了呢。但是'@me'更高级的是它是配合在[field:xxx]等标签中使用的,'@me'不仅负责输出,其本身也会保存着上层标签查询数据库后所得的内容,比如本段代码'@me'保存的就是取出的文章信息里'flag'的值,因此"@me = 'h'"这个判断才成立。

2、循环判断{dede:arclist}取出的标题是否为当天发布

Box-sizing:border-Box;outline:0px;list-style-type:decimal;background-color:#FFFFFF;">
Highlighter_387931" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;font-size:14px !important;min-height:auto !important;background-image:none !important;background-attachment:initial !important;background-size:initial !important;background-origin:initial !important;background-clip:initial !important;background-position:initial !important;background-repeat:initial !important;">
PHP=
 
 
文章是今天新发布的'
文章不是今天新发布的'
代码的基础,理解这个就不难了,这里不再赘述了。发这两个热身主要是为了来个有点点难度的,那就是本文最开始贴图里显示的,对{dede:arclist}取出的文章标题做判断,如果是头条推荐则在标题后面显示top图标,如果是当天新发布的则在标题后面显示new图标,二者都不属于的则只显示文章标题,见代码
Highlighter_344668" class="SyntaxHighlighter " style="width:651.406px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;background:none !important;">{dede:arclist row=4 orderby=pubdate typeid=2 titlelen=  ".@me[ ";
 ";
"
代码虽然长点,但是有前两段代码的基础看懂这个还是没什么难度的,代码中包涵的html内容不影响整个功能,仅是用来是布局相对美观使用,阅读程序代码时可以忽略掉。这里就可以体现出[field:array]的好处了,之前我们用[field:flag]也好,用[field:senddate]也好,都只是单一的判断,如果想做多条件判断,那就非[field:array]莫属了,这里取出之后的'@me'不仅包涵了'flag'和'senddate'信息,还包含有其他底层字段很多信息,下面所列出的都有:
Highlighter_761754" class="SyntaxHighlighter " style="width:686.063px;margin:1em 0px !important;padding:1px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:relative !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;line-height:20px !important;font-family:Consolas,monospace !important;margin:0px !important;padding:0px !important;border:0px !important;outline:0px !important;float:none !important;vertical-align:baseline !important;position:static !important;left:auto !important;top:auto !important;right:auto !important;bottom:auto !important;height:auto !important;width:auto !important;min-height:auto !important;background:none !important;">ID(同id),typeid,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,color,

猜你在找的Dedecms相关文章