From ColdFusion 11, you can now use all the relevant ColdFusion tags within CFScript blocks. In the versions before ColdFusion 11, most of the ColdFusion tags were not available in the script syntax. If you are coding in script block and required to call a tag, you had no choice but to come out of script block and use tag syntax.

With script support for CF tags added to ColdFusion 11, you can now write all your code in a CFSCRIPT block itself.

Script support is available for both simple tags as well as nested tags. For instance, you can use a simple CFHTTP tag in a CFScript block (refer /script/http_simple.cfm), as follows:

<cfscript>
    cfhttp(url=”www.google.com”, method=”GET”);
</cfscript>

You can also invoke a CFHTTP tag passing additional information through CFHTTPPARAM, making it a nested call, as shown below:

<cfscript>
       Cfhttp(URL=http://#CGI.SERVER_NAME#.../target.cfm, method=”GET”)
       {
             cfhttpparam(type="url", name='emp_name' value=”Jacob”);
             cfhttpparam(type="header", name='myheader' value=”My custom header”);
       }
       Writeoutput(cfhttp.filecontent);
<cfscript>

As general syntax for script support, a ColdFusion tag is invoked like a function call in CFSCRIPT block with tag name is used for the function name. The tag attributes are passed as comma separated name-value pairs to it, like arguments are passed to a function. The child tag (and the body in general) is defined within a curly brackets, just like a function block.

Script support for custom tags

The script syntax is also extended to invoke custom tags. You can invoke a custom tag in ColdFusion using two methods and both of them are supported for CFSCRIPT block. 

  • Cfm file based custom tags
  • Prefix based custom tags  

Example of a cfm file based custom tag

In this example, a custom tag happybirthday.cfm is called from custom1.cfm, as follows:

<cfscript>
   // cfm file based custom tags
      Cf_happybirthday(name=”john”, birthdate=”December 5, 1987”);
</cfscript>

Example of a prefix based custom tag

You can invoke prefix based custom tag invocation. You first import a taglib and define a prefix to use custom tags under your taglib folder. A custom tag file validateUser.cfm is present under securityTags folder. This custom tag is used with prefix “security”, as follows:

<cfscript>
    // Prefix based custom tags
     cfimport(taglib="securityTags",  prefix="security");
   security:validateUser(name=’John’);
</cfscript>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy