如果<cfoutput>什么都不返回,如何忽略?</cfoutput>

时间:2013-04-15 09:47:44

标签: coldfusion

我在表中放置了<cfoutput>个地址变量。我遇到的问题是这些变量有时为空,但由于换行符,它们仍占用表中的空间。

   <td>  
    <cfoutput>
       #getMeeting.meetingDemographicsAddressLine1#<br />
       #getMeeting.meetingDemographicsAddressLine2#<br />
       #getMeeting.meetingDemographicsCity#
    </cfoutput>
   </td>

如果变量为空,如何让浏览器忽略<br />标记?

2 个答案:

答案 0 :(得分:5)

您可以使用cffunction创建打印行函数,并使用cfif检查值的长度是否超过0.即

请确保您的确意味着null,请参阅http://www.bennadel.com/blog/1654-Learning-ColdFusion-9-IsNull-And-Working-With-NULL-Values.htm

<强>实施例

<cffunction name="PrintLine" returntype="void">
  <cfargument name="Value" />
  <cfif len(arguments.Value) GT 0>
    <cfoutput>#arguments.Value#<br /></cfoutput>
  </cfif>
</cffunction>

或从函数返回: -

<cffunction name="PrintLine2" returntype="string">
  <cfargument name="Value" />
  <cfset var foo = "" />
  <cfif len(trim(arguments.Value)) GT 0>
    <cfset foo = arguments.Value & "</br />" />
  </cfif>
  <cfreturn foo />
</cffunction>

可选择根据需要添加返回类型/提示/必需属性

<强>文档

请参阅http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_21.html

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_i_01.html

然后你可以这样做: -

<td>  
  <cfoutput>
   #PrintLine(getMeeting.meetingDemographicsAddressLine1)#
   #PrintLine(getMeeting.meetingDemographicsAddressLine2)#
   #getMeeting.meetingDemographicsCity#
  </cfoutput>
</td>

答案 1 :(得分:0)

    <cfoutput>
    <cfif len(getMeeting.meetingDemographicsAddressLine1) gt 0 >
           #getMeeting.meetingDemographicsAddressLine1#
    </cfif>
    <cfif len(getMeeting.meetingDemographicsAddressLine2) gt 0 >
           <br />#getMeeting.meetingDemographicsAddressLine2#
    </cfif>
    <cfif len(getMeeting.meetingDemographicsCity) gt 0 >
            <cfif len(getMeeting.meetingDemographicsAddressLine1) gt 0 or  len(getMeeting.meetingDemographicsAddressLine2) gt 0>
                 <br />#getMeeting.meetingDemographicsCity#
            <cfelse>
                #getMeeting.meetingDemographicsCity#
            </cfif>
    </cfif>
</cfoutput>