Andrew
Andrew

Reputation: 395

ColdFusion if then statement related to HTML

Hi I am pretty new to ColdFusion. My company still uses it. I have the following code I have tweaked. It seems to be working fine.

Basically I want to say where CountyName is null, do not display the county HTML code. When CountyName is not null, do show it.

<cfif isdefined("URL.LOCAL") and isdefined("URL.STATE_NO") is "true">
  <!----- If Local is the County (Contains the word County), the local 
   will be the County. (Do not want duplicate County results) ------>

  <cfif "#URL.LOCAL#" CONTAINS "County">
    <cfset LocalName="#URL.LOCAL#">
    <cfset StateNo=#URL.STATE_NO#>

    <!----- If Local is the City (does not contain the word County), 
    add the County code in addition to the City --->
  <cfelse> 
    <cfset LocalName="#URL.LOCAL#">
    <cfset CountyName="#URL.COUNTY#">
    <cfset StateNo=#URL.STATE_NO#>
  </cfif>

<cfelse> 
  <cfset LocalName="Madison">
  <cfset StateNo=1>
</cfif>

Update:

I just tried the following and it works fine in my HTML:

<cfif isdefined("URL.COUNTY") is "true">
  <p class="reportHeader_fontSemiBig">
    <cfoutput>#CountyName#</cfoutput> Property Codes
  </p>
<cfelse>
  <p>No county info</p>
</cfif>

Upvotes: 2

Views: 222

Answers (3)

Frank Tudor
Frank Tudor

Reputation: 4534

I think you are looking for:

<cfif isdefined("url.county") and url.county NEQ ''>
<p class="reportHeader_fontSemiBig">
<cfoutput>#CountyName#</cfoutput> Property Codes </p>
<cfelse>
<p>No county info</p>
</cfif>

Upvotes: 2

Pothys Ravichandran
Pothys Ravichandran

Reputation: 345

<cfif isdefined("url.county") and url.county NEQ ''>
<p>
<cfoutput>#url.County#</cfoutput> Property Codes </p>
<cfelse>
<p>No county info</p>
</cfif>

Upvotes: 0

Matt Busche
Matt Busche

Reputation: 14333

This is similar to the existing answer, but I'm going to explain things a little bit.

isDefined("url.county") just checks that the key county exists in the url scope. It doesn't check that there is a value. In ColdFusion you can drop the is true or gt 0 for evaluations and just check that the key exists and that there is a length to the key. If it was all spaces this would return false.

<cfif isdefined("URL.COUNTY") AND len(trim(url.county))>
  <p class="reportHeader_fontSemiBig">
    <cfoutput>#CountyName#</cfoutput> Property Codes
  </p>
<cfelse>
  <p>No county info</p>
</cfif>

In earlier versions of ColdFusion isDefined() used to be quite slow, but that's not the case anymore, but following best practice I would recommend using structKeyExists(url, 'County') instead of isDefined("url.county")

Upvotes: 4

Related Questions