CFLoop attributes for isFirst and isLast

Something that I’ve always loved in Angular is the ngRepeat directive provides a way to access the isFirst and isLast properties of the iteration. It could be cool for ColdFusion to do the same thing:

<cfloop
    item="contact"
    array="#data.contacts#"
    isFirst="isFirstContact"
    isLast="isLastContact">

    <!--- .... render contact .... --->

    <cfif ! isLastContact>
        <hr />
    </cfif>

</cfloop>

I don’t need this all the time. But, it’s much nicer than having to store in the index value and check to see if (index < contacts.len()).

I think this would work just as nicely for other loop-types (query, struct, list, etc).

Thoughts?

7 Likes

I will second this suggestion!

2 Likes

MEME

4 Likes

Your loop above could be done with css… thinking of other use case…

contact{
    position: relative;
}

contact::after {
    content: '';
    position: absolute;
    width: 100vw;
    height: 1px;
    left: 0;
    display: block;
    clear: both;
    background-color: black;
}

or to fancy your lines up add these : Simple Styles for Horizontal Rules | CSS-Tricks - CSS-Tricks

I’ve definitely used CSS and things like :last-child to alter the way something happens in a list. But, this isn’t always possible (such as when the thing being added needs complex HTML structure). But yeah, I do like using CSS when possible as it reduces the complexity in the code.

I doubt this would be adopted the original code is more readable IMHO, plus cfloop already has a kitchen sink of attributes <cfloop> :: Lucee Documentation

That’s for sure! It’s kind of bananas how much that one little tag does.