Search code examples
coding-stylecoldfusionconventions

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?


Solution

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