volume one
volume one

Reputation: 7563

Is it safe to wrap an entire CFM page in a cfoutput tag

I am placing a <cfoutput> tag around my entire <html> tag. The ColdBox best practice guide states "When you are creating view templates, try to always surround it with 1 cfoutput tag, instead of nesting them all over the place."

But I have on occasion seen errors pop up where a <script> block containing javascript code is within the <cfoutput> tag. This probably because Coldfusion sees a hash # and tries to parse it but it can't because its javascript.

So how does one get away with having a single <cfoutput> tag on a view page in which to place everything?

Upvotes: 5

Views: 497

Answers (3)

Steve Withington
Steve Withington

Reputation: 480

The simple answer to your question as posted is yes.

There are only two issues that I'm aware of to keep in mind:

  1. Escape any single hashtags (#) with double hashtags (##) that may occur in your code (i.e., CSS, JS, etc.) ... unless the hashtags are actually being wrapped around a CFML function or variable.
  2. If you're using "cfoutput query...", you will probably want to close the first "cfoutput" tag, and then reopen after the query output. Otherwise, you can run into issues when trying to group query output.

My preference is to use as few tags as possible, mostly for readability and to reduce clutter.

Upvotes: 0

Brad Wood
Brad Wood

Reputation: 3953

I am not aware of any significant security or performance concerns in regards to wrapping an entire page in cfoutput. Of course, you'll always need to be aware to escape any pound signs by doubling them up any time you're inside a cfoutput.

The best practices in that ColdBox guide are geared primarily toward readability and reducing clutter on the page. If you have large sections of the page that you don't want to escape pound signs on or if you like to use cfoutput's grouping functionality, there's nothing wrong with breaking up your cfoutputs in a way that makes sense.

In the olden days of CF there might have been more overhead, but these days I can't imagine it being more than a few nanoseconds, and that's once at compile time.

Upvotes: 3

Simon Bingham
Simon Bingham

Reputation: 1310

In my view files I tend to wrap all output in a single cfoutput tag.

You can escape # symbols in JavaScript, etc, by converting them to ##.

Upvotes: 2

Related Questions