Is it good practice to end coldfusion self-closing tags with "/>"?
Asked Answered
C

4

6

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?

Crespi answered 25/9, 2011 at 8:20 Comment(2)
I think it is a good practice. Screw one keystroke. I prefer readability. If I don't see the /> I want to know where the end tag is.Scapula
Its late but i'll add. When using code reformatting, unclosed tags gets messed up. So its not only good practice its useful with code formatting tools.Fishmonger
B
11

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>&lt;cf_demo&gt;</p>

<cf_demo>

<p>&lt;cf_demo /&gt;</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... :)

Birefringence answered 25/9, 2011 at 9:54 Comment(1)
<cfmodule> behaves this way--and if you are going away from the "I install all my CustomTags in a single directory" model (most likely used by a shared host), you'll end up calling your CustomTags via <cfmodule>, and Sergii's answer becomes a very real issue. Personally, as long as you are aware of those gotchas, I absolutely approve of the XHTML-style of properly closing tags with />.Carbonic
K
0

It doesnt matter, it is also not necessary in html unless it is xhtml.

Knoll answered 25/9, 2011 at 8:35 Comment(2)
Oops, think I've missed that It doesn't matter is actually about CFML. Sorry.Birefringence
it is also not necessary in html unless it is xhtml "for self-closing tags"Exum
U
0

I agree with the last comment.

I hate those single tag closes.

It's pointless and not a coding standard for CFML.

It started appearing when xml became popular due to it's strict tag syntax and people assuming it was correct for CFML. CFML isn't HTML. Treating it as such is really in itself lazy coding. I also think it looks more beauiful without the unnecessary closing /> :) but that's me for you.

I also dislike {} spread on to new lines for each bracket. I guess it's just personal preference.

Unloosen answered 26/11, 2014 at 17:3 Comment(0)
O
0

I never used to use the /> until i started using Dreamweaver CC and the auto close only works if you close the tags somehow

Orectic answered 20/10, 2022 at 14:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.