Converting an ASP to CFM an Example

Here's an ASP program that displays the current Month, Day and Year in the option box plus a populated routine to display other values for Months, Day and Year (from 1998 to 2010). The program is neatly done (thanks to Jonhcy George) and I thought of converting it to CFML for our consumption (without his permission :-!).

My Funny Findings in ASP

  • ASP sorrounds it's variable initialization with <% %> in a bunch.
  • the LOOP controls, IF and other ASP statements again are sorrounded with <% %>

I'd like to appreciate how asp uses the <% %> tag to group asp statements it's much the same way as when you one it's an asp! Wouldn't it be nice to see the code well documented and group according to it's use?.

When you want to comment a bunch of text in CFML you say <!--- text ----> while in ASP, there's no difference to using <% %> to other statements elsewhere be it a comment block, assigning values to the variables and writing an output statement.

For me, ASP's approach to using <% %> will complicate the coding process especially for the beginners ditto with like in CFML's use of pound signs. Though we we've learned the trade already (thanks to the online sample usage and some more examples contributed elsewhere).

What are my replacements?

There are obvious statements in ASP that can easily be substituted with CFML's like Date() with Now(), MonthName() with MonthAsString() and For...Next structure with <CFLoop index...from...to..></CFLOOP>


The ASP Program
(with my comment in this
color)

<% REM - For displaying date and selecting the current date. %>
(just a a comment line)

<% iMonth = Month(Date)
iDay = Day(Date)
iYear = Year(Date)%>
(The 3 lines above are an obvious variable initialization.)

<select name="lstMonth" size="1">
(needless to explain)

(here's the code to show the months. Oh boy, here goes the loop)
<% for i=1 to 12 %>
<option value="<% =i %>"
<% if iMonth=i then %> SELECTED <% end if %>>
<% =MonthName(i) %>
</option>
<% next %>

</select>

<select name="lstDate" size="1">

(and to show the Days)
<% for i=1 to 31 %>
<option value="<% =i %>"
<% if iDay=i then %> SELECTED <% end if %>>
<% =i %>
</option>
<% next %>

</select>

<select name="lstYear" size="1">

(finally the Years)
<% for i=1998 to 2010 %>
<option value="<% =i %>"
<% if iYear=i then %> SELECTED <% end if %>>
<% =i %>
</option>
<% next %>

</select>


Our version of CFML

<!--- REM - For displaying date and selecting the current date. --->
<cfset iMonth = #Month(now())#>
<cfset iDay = #Day(now())#>
<cfset iYear = #Year(now())#>
<form>

<select name="lstMonth" size="1">

<cfloop index="i" from="1" to="12">
<CFoutput>
<option value="#i#"
<cfif #iMonth# eq #i#>SELECTED>
<cfelse> >
</cfif>
#MonthAsString(i)#
</option>
</CFoutput>
</cfloop>

</select>

<select name="lstDate" size="1">

<cfloop index="i" from="1" to="31">
<CFoutput>
<option value="#i#"
<cfif #iDay# eq #i#>SELECTED>
<cfelse>>
</cfif>
#i#</option>
</CFoutput>
</cfloop>

</select>

<select name="lstYear" size="1">

<cfloop index="i" from="1998" to="2010">
<CFoutput>
<option value="#i#"
<cfif #iYear# eq #i#>SELECTED>
<CFELSE>>
</cfif>
#i#</option>
</CFoutput>
</cfloop>

</select>
</form>


[ The Code Factory Home ] [ back to CF Tips ]

1