Date, Media, and Text Helpers
Wheels includes a plethora of view helpers to help you transform data into a format more easily consumed by your applications' users
Wheels's included view helper functions can help you out in those tricky little tasks that need to be performed in the front-end of your web applications. Although they are called miscellaneous, they are in fact categorized into 3 categories:
- Date Helpers
- Media Helpers
- Text Helpers
Wheels does a good job at simplifying the not so fun task of date and time transformations.
Let's say that you have a comment section in your application, which shows the title, comment, and date/time of its publication. In the old days, your code would have looked something like this:
#DateFormat(comments.createdAt, "mmmm d, yyyy")#
#LCase(TimeFormat(comments.createdAt, "h:mm tt"))#</p>
That works, but it's pretty tedious. And if you think about it, the date will be formatted in a way that is not that meaningful to the end user.
With that minimal change, you have a prettier presentation for your end users. And most important of all, it didn't require you to do anything fancy in your code.
Working with media is also a walk in the park with Wheels. Let's jump into a few quick examples.
<!--- layout.cfm --->
This will generate the
<link>tag for you with everything needed to include the file at
If you need to include more than one style sheet and change the media type to "print" for another, there are arguments for that as well:
Lastly, you can also link to stylesheets at a different domain or subdomain by specifying the full URL:
With this simple call, Wheels will generate the
images/logo.pngand also set the
altattributes automatically for you (based on image dimensions and the image file name). Wheels will also cache this information for later use in your application.
If you need to override the
altattribute for better accessibility, you can still do that too:
#imageTag(source="logo.png", alt="ColdFusion on Wheels")#
To illustrate what the text helpers can help you with, let's see a piece of code that includes 2 of the text helpers in a simple search results page.
<!--- Query of search results --->
<cfparam name="searchResults" type="query">
<!--- Search query provided by user --->
<cfparam name="params.q" type="string">
#highlight(text="Your search for #params.q#", phrases=params.q)#
That code will highlight all occurrences of
params.qand will pluralize the word "result" to "results" if the number of records in
searchResultsis greater than 1. How about them apples? No
<cfif>statements, no extra lines, no nothing.
The functions we have shown in this chapter are only the tip of the iceberg when it comes to helper functions. There's plenty more, so don't forget to check out the View Helper Functions API.