Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 210
froadie Avatar asked Sep 25 '11 08:09

froadie


People also ask

Should I close self-closing tags?

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.

Why is it important to use closing tags?

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.

In which situation can you use self-closing tags?

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.

Do you need to close a tag?

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).


1 Answers

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... :)

like image 189
Sergey Galashyn Avatar answered Sep 20 '22 14:09

Sergey Galashyn