November 30, 2020
Strict Equality/In-Equality(===/!==) Operator and SaveContent New Syntax
Comments
(0)
November 30, 2020
Strict Equality/In-Equality(===/!==) Operator and SaveContent New Syntax
Newbie 6 posts
Followers: 3 people
(0)

Strict Equality/In-Equality Operator

Object Comparison is one of the fundamental concepts in any language. ColdFusion also have many operators and functions to do object Comparison. While this has been the case, there were some problems faced by ColdFusion developers while comparing two objects viz true/1/”yes” means the same thing and so while trying to compare these values against each other, it would always return true. 

Coldfusion had the ability to compare two objects using the == or EQ oprerator. In this release we have implemented a Strict Equality Operator(===) and Strict In-Equality Operator(!==). The motivation behind implementing the === and !== operators was that ColdFusion from the very beginning was a loosely typed language, where-in the engine tries to smartly interpret the type of variable. In ColdFusion 2018, ColdFusion came in with the ability to preserve the data types which solved a lot of problems, however due to backward compatibility the equality operator was still comparing two objects by its values and not by types. Hence it became a need for the language to introduce a Strict Equality and Strict In-Equality Operators to cater to the problems which could not be solved by == or EQ operator.

With Project Stratus(ColdFusion 2021) release, we have added the Strict Equality(===)/In-Equality(!==) , which would first compare the values based on types and then the actual values.

Lets take some examples which will highlight the broader use-case of this functionality

<!---
    Strict Equality ===
--->
 
<cfscript>
writedump(2=="2");  // YES
writedump(2==="2"); // NO
writedump('yes' == 1); // YES
writedump('yes' === 1);// NO
writedump(false ==0); // YES
writedump(false ===0);//NO
</cfscript>
 
 
<!---
    Strict InEquality !==
--->
 
<cfscript>
writedump(2!="2");  // NO
writedump(2!=="2"); // YES
writedump('yes' != 1); // NO
writedump('yes' !== 1);// YES
writedump(false !=0); // NO
writedump(false !==0);//YES
</cfscript>
 
Save Content New Syntax

SaveContent has been in the ColdFusion Mark up Language and is widely used to save the generated content inside a block to a variable. However the syntax was old and redundant due to which it has been revamped.

Old Syntax:

savecontent variable="myContent" writeOutput("Sample Content"); }

New Syntax:

myContent = savecontent{
writeOutput("Sample Content");
}

The new syntax gives a ColdFusion developer the ability to initialize the content inside a savecontent block to a LVAR, where LVAR can be a simple variable reference or an array/struct keys reference.

Lets simulate it with an example:
 
<!-- Assigning the content of Save Content to
Mix and match of Array and Struct Notation
--->
<cfscript>
 
test1.key1['key2']['key3'].key4= savecontent {
WriteOutput('<table border="0" cellpadding="0" cellspacing="0">
<tr><td bgcolor="ff0000" width="50">&nbsp;</td><td bgcolor="##0000ff">&nbsp;</td></tr>
<tr><td bgcolor="##ffff00">&nbsp;</td><td bgcolor="##000000"  width="50">&nbsp;</td></tr>
</table>');
}
</cfscript>
<cfoutput>#test1.key1.key2.key3.key4#</cfoutput>
 
<!-- Assigning the content of Save Content to
Array Reference.
--->
<cfscript>
test1['key1']['key2']['key3']['key4']= savecontent {
WriteOutput('<table border="0" cellpadding="0" cellspacing="0">
<tr><td bgcolor="ff0000" width="50">&nbsp;</td><td bgcolor="##0000ff">&nbsp;</td></tr>
<tr><td bgcolor="##ffff00">&nbsp;</td><td bgcolor="##000000"  width="50">&nbsp;</td></tr>
</table>');
}
</cfscript>
<cfoutput>#test1.key1.key2.key3.key4#</cfoutput>
 
 
<!-- Assigning the content of Save Content to
Struct Reference
--->
<cfscript>
test1.key1.key2.key3.key4= savecontent {
WriteOutput('<table border="0" cellpadding="0" cellspacing="0">
<tr><td bgcolor="ff0000" width="50">&nbsp;</td><td bgcolor="##0000ff">&nbsp;</td></tr>
<tr><td bgcolor="##ffff00">&nbsp;</td><td bgcolor="##000000"  width="50">&nbsp;</td></tr>
</table>');
}
</cfscript>
 
<cfoutput>#test1.key1.key2.key3.key4#</cfoutput>
 
The above code will output as shown below:
 
0 Comments
Add Comment