Scripting Syntax

The <cfscript> and </cfscript> tags are at the heart of the CFScript language. All CFScripting in a template takes place between these tags. There are a few general guidelines that will help you write your CFScripts:

  • cfscript tag pairs can be placed anywhere in your template.

  • More than one set of cfscript tags can be placed in your template.

  • cfscript tags may not be nested.

  • Code (including variable names) placed between cfscript tag pairs is case-insensitive.

  • CFML tags may not be used within a cfscript tag block.

  • CFML functions may be used within a cfscript block.

  • CFML variables created outside a cfscript block are automatically available within the block.

  • Not all CFML variables created inside the cfscript block are automatically available outside of the block.

  • CFScript statements end in a semicolon.

  • Curly braces ({ }) are used to group related statements.

Working with Variables

Assigning values to variables in CFScript is simple. Assignment statements take the following form:

               variable = expression;

Here, variable is the name of the variable, and expression is the value you wish to assign to the variable. The semicolon marks the end of the statement. For comparison sake, consider the following example in which we assign values to a number of variables using the cfset tag:

<cfset x = 1>
<cfset y = 1+2>
<cfset MyMessage = "Hello World">
<cfset MyStructure = structNew( )>

The same functionality can be rewritten using CFScript syntax like so:

<cfscript> x = 1; y = ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.