A frequent task in RedDot Open Text CMS template construction is to assign the value of a placeholder to a string so you can perform some kind of logic on it. Take this example:
<% dim label
label = “<%stf_Label%>”
%>
<!/IoRangePreExecute>
If the <%stf_Label%> placeholder happens to contain an un-encoded quote, you’ll get a server error. But sometimes you just really NEED a quote in the value. How do you handle this?
Strange Bedfellows
Server side JavaScript (or JScript) has long been supported, but rarely used, by Internet Information Server. In this case we can use the following code to correctly capture the value.
<script language=”javascript” runat=”server”>
var PlaceholderValueRegExp = /^\(?function\s?([_a-zA-Z][_a-zA-Z0-9]*)?\(\s*\)\s*\{\s*\/\/([\s\S]*)\}\s*\)?\s*$/i;
function GetPlaceholderValue(PlaceholderFunctionString) {
var str = PlaceholderFunctionString.match(PlaceholderValueRegExp);
if(str != null && typeof(str) == “object”)
return str[2];
else
return “”;
}
if(!stf_Label<%PageID%>)
function stf_Label<%PageID%>(){
//<%stf_Label%>
}
</script>
<%
dim label
label = GetPlaceholderValue(stf_Label<%PageID%>.toString())
%>
<!/IoRangePreExecute>
And presto – you have your variable and your quotes too.
What’s Behind the Magic Curtain?
This code combines several techniques in a not-so-straightforward manner to get our desired result. Here’s the play-by-play:
In this section, assume the <%PageID%> info element is the value 1001 and <%stf_Label%> has the value My “Quoted” placeholder.
- Using JScript’s toString() method on a function.
In the example above, we call stf_Label<%PageID%>.toString() in the ASP, but stf_Label<%PageID%> is a function defined in JScript! Yes you can call JScript functions in VBScript. By calling the “toString()” method on a function in JScript, we get the function body returned as a string. In this case“function stf_Label1001() { //My \”Quoted\” placeholder }”. - Regular Expression Matching.
Using our function body string, we use the PlaceholderValueRegExp regular expression to find the placeholder value after the // characters.
Now we have a string value that properly contains a quote.
Putting it all together
In a real-world situation, you will want to put the GetPlaceholderValue function (and the preceding RegEx) in your master or foundation page so that it is only defined once. The extra if statement before the function stf_Label<%PageID%>() statement allows this code to be placed in a template that may occur multiple times on the same page.