In HTML, I was always taught to close self-closing with a "/>". For example "<br />
", "<input type='button' value='myButton' />
", etc.
In Coldfusion, though, it seems to be standard to just never close these tags. I'm constantly seeing code like:
<cfset myVariable = someValue>
<cfset myOtherVariable = someOtherValue>
etc.
Is this bad code, or is it commonly accepted? I've seen it almost anywhere that I've seen coldfusion code. Is there any benefit to closing these tags, or is it fine to leave it as it is?
Self-closing tags do not necessitate a closing tag. For example, <input> is a self-closing tag. Self-closing tags do not contain any content. They are either empty or receive data from attributes to render.
Should Optional HTML Tags be Closed? Code with closing tags is much more readable and easy to follow. It is much easier to visually inspect a page with well laid out markup. Working with this markup is easier for developers.
A self-closing tag is an element of HTML code that has evolved in the language. Typically, the self-closing tag makes use of a “/” character in order to effectively close out a beginning tag enclosed in sideways carets.
A tag must always be closed by the tag close symbol > (if we ignore certain SGML rules that nominally apply in non-XHTML HTML but were never implemented in browsers).
Because there's no official coding standard for CFML, it's up to you whether to use these. Same as using uppercase/lowercase tags.
Personally I love to have my code beautiful and readable, so I'm always using this syntax for single tags.
But there is at least one techincal difference: custom tags. Let me show this by example.
Consider following custom tag:
<cfif thisTag.ExecutionMode EQ "start">
started<br/>
</cfif>
running<br/>
<cfif thisTag.ExecutionMode EQ "end">
ended<br/>
</cfif>
Now these two types of invokation:
<p><cf_demo></p>
<cf_demo>
<p><cf_demo /></p>
<cf_demo />
And here's the output:
<cf_demo>
started
running
<cf_demo />
started
running
running
ended
Second syntax is equivalent of <cf_demo></cf_demo>
.
Possibly there are more differences, but I can't remember any at this moment... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With