Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Collecting HTML Class Name Attributes In Template Rendering In Lucee CFML 5.3.7.47
<cfloop index="local.scheduleDay" array="#rc.scheduleDays#">
<li class="m-schedule__item #( scheduleDay.isActiveDeployment ? 'm-schedule__item--deployment' : '' )# #( scheduleDay.isToday ? 'm-schedule__item--today' : '' )# #( scheduleDay.isFuture ? 'm-schedule__item--future' : '' )# #( scheduleDay.isBlocked ? 'm-schedule__item--blocked' : '' )#">
<!--- truncated --->
</li>
</cfloop>
<li [ngClass]="{
'm-schedule__item': true,
'm-schedule__item--deployment': scheduleDay.isActiveDeployment,
'm-schedule__item--today': scheduleDay.isToday,
'm-schedule__item--future': scheduleDay.isFuture,
'm-schedule__item--blocked': scheduleDay.isBlocked
}">
<!-- truncated -->
</li>
<cfscript>
contacts = [
{ id: 1, name: "Joe", isBFF: true, isFriend: true },
{ id: 2, name: "Sam", isBFF: false, isFriend: true },
{ id: 3, name: "Kit", isBFF: false, isFriend: false },
{ id: 4, name: "Ryan", isBFF: false, isFriend: false },
{ id: 5, name: "Alex", isBFF: true, isFriend: true },
{ id: 6, name: "Jordan", isBFF: false, isFriend: true }
];
```
<cfoutput>
<ul>
<cfloop value="contact" array="#contacts#">
<li class="#encodeClassAttribute([
contact: true,
'contact--friend': contact.isFriend,
'contact--bff': contact.isBFF
])#">
#encodeForHtml( contact.name )#
</li>
</cfloop>
</ul>
</cfoutput>
```
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I collect the list of CSS class-names to apply to a given context. The list will
* include every key whose value is a Truthy.
*
* @conditions I am the key/value pairs being inspected.
*/
public string function encodeClassAttribute( required struct conditions ) {
var classNames = [];
loop
key = "local.className"
value = "local.condition"
struct = conditions
{
if ( isTruthy( condition ?: false ) ) {
classNames.append( className );
}
}
return( classNames.toList( " " ) );
}
/**
* I determine if the given value is a Truthy.
*
* @value I am the value being inspected.
*/
public boolean function isTruthy( any value ) {
return( ! isFalsy( value ) );
}
/**
* I determine if the given value is a Falsy.
*
* @value I am the value being inspected.
*/
public boolean function isFalsy( any value ) {
if ( isNull( value ) ) {
return( true );
}
if ( ! isSimpleValue( value ) ) {
return( false );
}
if ( isBoolean( value ) || isNumeric( value ) ) {
return( ! value );
}
return( value == "" );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment