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.
- Modern Campus Calendar Details - April 2, 2024
- Calendar updates: Training available - March 20, 2024
- Updates: New Modern Campus Calendar - March 7, 2024
- Preview MSU’s new calendar system - January 22, 2024
- Recap: Hello Omni CMS, goodbye Web Press! - March 14, 2023
- Improve your Omni CMS skills at this free training - February 21, 2023
- Celebrate Global Accessibility Awareness Day (GAAD) on May 19 - May 16, 2022
- New Content Management System Coming Soon - March 3, 2021
- Meet our newest team member: Valerie Cummings - October 20, 2020
- Graduate students: Share the stories of MSU - May 1, 2019