ColdFusion Content Injection
In many systems, it is nice to be able to have pre-written messages that can be injected with customized content at runtime.
This tutorial will explain how to write a basic content injection system.
We'll just build a query to hold some sample system messages that we need to customize at runtime. We'll add some extra fields as placeholders for the values we want to inject into the message.
<cfscript>
qMessage = querynew("messageid,messagetitle,message","integer,varchar,varchar");
//first sample row
queryaddrow(qMessage);
querysetcell(qMessage,"messageid",1);
querysetcell(qMessage,"messagetitle","Registration Complete");
querysetcell(qMessage,"message","Thankyou [customername], registration is complete");
//second sample row
queryaddrow(qMessage);
querysetcell(qMessage,"messageid",2);
querysetcell(qMessage,"messagetitle","New Ticket");
querysetcell(qMessage,"message","Dear [customername], Ticket number [ticketnbr] has been created.");
</cfscript>
You'll notice that there are several key words in brackets in the message. (i.e. [customername], [ticketnbr]) - These are the placeholders that will be populated at runtime.
The code below will query the messages we just created and display a select box to allow you to choose a message:
<cfquery name="getMessages" dbtype="query">
select messageid, messagetitle,message
from qMessage
order by messagetitle
</cfquery>
<form method="get">
<select name="messageid">
<cfoutput query="getMessages">
<option value="#getMessages.messageID#">#getMessages.messagetitle#</option>
</cfoutput>
</select>
<input type="submit" value="go" />
</form>
In order to prefill the message, we create a struct with keys that match the ones that are expected in the message.
These values would typically be set from a query result or some other logic - in this case we just create it with some dummy data.
<cfset stInject = structnew()>
<cfset stInject.customername = "Nathan Miller">
<cfset stInject.ticketnbr = createuuid()>
Once the form is submitted, we get the selected message using query:
<cfquery name="getSelectedMessage" dbtype="query">
select message
from qMessage
where messageid = #url.messageid#
order by messagetitle
</cfquery>
The original message: <cfoutput>#getSelectedMessage.message#</cfoutput>
The guts of the system is the following UDF. This function takes a struct of data in the first argument, and tries to match the struct keys to the placeholders in the second argument. It returns the fully injected text as a string.
<cffunction name="injectContent" returntype="string" output="false">
<cfargument name="stContent" type="struct" hint="container of data that will inject into the raw text">
<cfargument name="rawtext" type="string" hint="text with placeholders that will be injected">
<cfset var lstFields = structkeylist(arguments.stContent)>
<cfset var placeholder = "">
<cfset var i = 0>
<cfset var rtn = arguments.rawtext>
<!--- there must be a matching struct key in arguments.stContent that matches a placeholder name -
the place holder will be filled with the stContent key value --->
<cfloop list="#lstFields#" index="placeholder">
<cfset rtn = replacenocase(rtn,"[#placeholder#]",arguments.stContent[placeholder],"all")>
</cfloop>
<cfreturn rtn />
</cffunction>
To inject the values into the message, just call the injectContent function:
<cfset newMessage = injectContent(stInject,getSelectedMessage.message)>
The new message: <cfoutput>#newMessage#</cfoutput>
Any unused keys will simply be ignored. For example, if you select the 'Reg Complete' message, you can still create
a new ticket number and pass it to the function, but since there is no [ticketnbr] place holder in the message, stInject.ticketnbr will be ignored.
You can download a fully functional demo here
-
Add a newsfeed to your site
Use the Moreover news service to add a news
feed to your site using cfhttp and wddx in 5 minutes!
Author: Nathan Miller
Views: 22,265
Posted Date: Saturday, July 26, 2003
-
Advanced Server-Side FORM Validation
An advanced set of customizable tags for validating FORM values on the Server-side using User-Defined Functions
Author: Nathan Miller
Views: 19,119
Posted Date: Monday, December 15, 2003
-
Automatically build a FORM using SQL Server system tables
Use SQL server's system tables to automatically create a form to insert data into your database.
Author: Nathan Miller
Views: 24,188
Posted Date: Monday, May 19, 2003
-
ColdFusion Content Injection
Inject customized content into predefined messages by adding specially formatted placeholders in the message.
Author: Nathan Miller
Views: 8,326
Posted Date: Wednesday, March 28, 2007
-
Coldfusion Master Pages
Use ASP.NET-style master pages to replace your header/footer files for web page layout
Author: Nathan Miller
Views: 9,894
Posted Date: Wednesday, August 30, 2006
-
DHTML Tabs (Part 1 of 2)
Visual tab control generator - Creates an interface that loads all the data in each tab in a single page request. This allows the users to click on each tab in the interface and instantly see the data on that tab without waiting for it to load.
Author: Nathan Miller
Views: 9,743
Posted Date: Wednesday, October 26, 2005
-
Dynamically add and remove a 'please wait' message to your complex pages
You can use a combination of and javascript to show a message on the screen
while processing a large data set (or any other long-running task), and then remove the message when the task is complete.
Author: Nathan Miller
Views: 31,087
Posted Date: Tuesday, January 13, 2004
-
Easy automatic debugging
Extend the use of the new tag in CF5
to display any and all ColdFusion and CGI variables.
Author: Nathan Miller
Views: 13,032
Posted Date: Saturday, December 21, 2002
-
Pass complex data in a hidden form field with Base64
Use Base64 to pass html or large blocks of text in a form post without using session variables.
Author: Nathan Miller
Views: 8,569
Posted Date: Friday, March 31, 2006
-
Scope your variables
Increase performace and readability by always scoping your variables
Author: Nathan Miller
Views: 21,360
Posted Date: Monday, May 19, 2003
-
The better way to handle session variables
You can use these simple loops in place of many lines of code to clear out or delete session variables
Author: Nathan Miller
Views: 21,309
Posted Date: Monday, May 19, 2003