Coldfusion CFSCRIPT  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Coldfusion
Written by Bogdan V   
Saturday, 12 January 2008


In this example I will show you two types of modern scripting for the web.


  • Block tags
  • Whatever is inside will be executes (evaluation zone)
  • No tags allowed
  • Each expression must be terminated by a semi-colon (;)
  • Assignment is simply variablename=value

<CFSCRIPT>    Name="Michael";-----String
    Age=29;-----------------Number
    Date=Now();-----------Function
    Sum=11+7;------------Operation
    AgeNext=Age+1;------Expression
</CFSCRIPT>

  • 2 forms of comments
  • Comments needed as the code will be more obfuscated

<CFSCRIPT>
    // This is a one line comment. Use this to comment on a code snippet

    /*
        This is a multi-line comment
        Use this when writing docs at the top of a CFACRIPT block
    */
</CFSCRIPT>



Execution functions (see handout) DO NOT NEED to be assigned to a variable, especially in CFSCRIPT

<CFSCRIPT>
    // This will clear the session structure
    StructClear(session);

    // This will disconnect the data sources
     cfusion_dbconnections_flush();

     // Reset a query cell
     QuerySetCell(myquery, 'name', 'Michael', 1);
</CFSCRIPT>


  • Pay attention to standard Reserve Words
  • New Reserve Words

CFSCRIPT: If 

<CFSCRIPT>// Standard IF statment
IF (score GT 1)
    result = "positive";

// If statment with multiple parts in the if
IF (score GT 1)
    {
            result = "positive";
        totalscore=score+1;
    }

</CFSCRIPT>


CFSCRIPT: IF - Else

<CFSCRIPT>
// If with an Else
IF (score GT 1)
    result = "positive";
ELSE
    result = "negative";

// If, Else, Else If
IF (score GT 1)
    result = "positive";
ELSE IF (score EQ 0)
        result="null";
    ELSE
        result = "negative";
</CFSCRIPT>


CFSCRIPT: Switch

<CFSCRIPT>
    switch(name)
    {
        case "John":
        {
            male=true;
            break;
        }
        case "Mary":
        {
            male=false;
            break;
        }
        default:
        {
            found=false;
            break;
        }
    } //end switch
</CFSCRIPT>

CFSCRIPT: For Loop

For loop
    FOR (init-expr ; test-expr ; final-expr) statement ;
init-expr and final-expr can be
 single assignment expression, for example, x=5 or loop=loop+1
any ColdFusion expression, for example, SetVariable("a",a+1)
empty
The test-expr can be
any ColdFusion expression, for example, A LT 5, loop LE x, or Y EQ "not found" AND loop LT
empty

<CFSCRIPT>
    // Loop from 1 to 10
    for(Loop1=1; Loop1 LT 10; Loop1 = Loop1 + 1)
        score=score+loop1;

    // Loop from 1 to 10
    for(Loop1=1; ; Loop1 = Loop1 + 1)
        {
            score=score+loop1;
            IF (Loop1 EQ 10)
                break;
        }
</CFSCRIPT>


CFSCRIPT: While Loop

<CFSCRIPT>
    // While expression is true
    a = ArrayNew(1);
    while (loop1 LT 10)
        {
            a[loop1] = loop1 + 5;
            loop1 = loop1 + 1;
        }

    // While expression is true
    a = ArrayNew(1);
    while (loop1 LT 10)
        {
            IF (loop1 mod 2 EQ 0)
                continue;
            loop1 = loop1 + 1;
        }
</CFSCRIPT>


CFSCRIPT: Do-While Loop

<CFSCRIPT>
    // Multiline do-while loop
    // Tests AFTER code is run
    a = ArrayNew(1);
    do
        {
            a[loop1] = loop1 + 5;
            loop1 = loop1 + 1;
        }
    while (loop1 LT 10);
</CFSCRIPT>


CFSCRIPT: For-In Loop

Special loop for dealing with collections (I.e. structures)
for (variable in collection) statement ;

<CFSCRIPT>
    for (x in Session)
        WriteOutPut(Session[x]&’<br>’);
</CFSCRIPT>


User reviews

Average user rating from: 1 user(s)

Overall rating
5.0
 

Add new review



2 of 3 people found the following review helpful

Slight change to For-In Loop syntax, Tuesday, 11 November 2008

Written by Brent Shaub

Overall rating
5.0
Thanks for these examples. I had trouble finding an example of the For-In Loop flavor. The syntax above for the WriteOutput line needs double-quotes to replace the single ones. Then it worked wonderfully. Thanks again.
Was this review helpful to you? yes     no


Powered by jReviews

Last Updated ( Saturday, 12 January 2008 )
 
< Prev   Next >