String Templating in Flex
When working with String templates, code can easily become quite difficult to read and, consequently, to understand, maintain and modify. This blog post explains 2 techniques that can help you dealing with String concatenation and templating: StringUtil.substitue and [Embed] compiler directive.
For simple String concatenation like:
You can alwayus use StringUtil.substitue, which will help you having a wider vision of the message being built:
var value : String = StringUtil.substitute("The value {0} requested is \"{1}\"", user, value);
When creating multi-line Strings, things can easily get uglier:
var errorString:String;
var errorMessage:String;
var str:String = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
str += "<html><head>\n";
str += "<title>" + errorCode + " " + errorString + "</title>\n"
str += "</head><body>\n";
str += "<h1>" + errorCode + "</h1>\n";
str += "<p>" + errorMessage + "</p>\n";
str += "</body></html>";
Not to say that this could get worse. A way to make it a little bit more readable would be using the StringUtil substitution technique:
str += "<html><head>\n";
str += "<title>{0}{1}</title>\n"
str += "</head><body>\n";
str += "<h1>{0}</h1>\n";
str += "<p>{2}</p>\n";
str += "</body></html>";
StringUtil.substitute( str, errorCode, errorString, errorMessage);
The problem with this is that there’s still too much infrastructure to create the base template and it’s difficult, at first sight, to see where are the substitution tokens. On the other hand, the templated text by itself might not have that much relevance in the code and might distract developers from other imporntant things.
A nice way to achieve the same result consists in externalizing the template to a text file and get some help from the compiler:
private var templateDefinition : Class;
StringUtil.substitute( new templateDefinition(), errorCode, errorString, errorMessage);
Where yourTemplate.txt is a simple text file with the template itself:
<html><head>
<title>{0} {1}</title>
</head><body>
<h1>{1}</h1>
<p>{2}</p>
</body></html>
Note that using the [Embed] metadata instructs the compiler to link the file content at compile time, making our code completely synchronous.
Also note that we’re substituting the parameters using the order, it’d would fairly easy to do a named-parameter subsitution instead (i.e. using tokens like ${var1})
Finally, in MXML you can use this other technique to compile an external text file:
7 Comments to “String Templating in Flex”
Leave a Reply

Thanks!
Actually I was looking for something like that to use with Lupo Manager and localization!
[...] Source http://www.rialvalue.com/blog/2010/05/10/string-templating-in-flex/ [...]
Another way that works in a code file that makes String handling more natural: You can avoid concatenating lines of a string by newing a String and putting the contents of the string between
);
Example:
public var template1 : String = new String(
); // end of string
In my previous comment, the CDATA block was removed by the website. The code should have a CDATA block withing the contructor call of the String.
Xavi,
This is very nice except that I’m having a problem with the fx:String syntax you present at the very end. When I use this approach the string that is loaded does not have the curly braces surrounding the template parameter numbers. Therefore, the string substitution does not work. Here’s my code:
< fx:String id=”template” source=”yourTemplate.txt”/>
David Salahi
P.S. The RSS feed link at the top of this page doesn’t work.
Hi David,
I’m just in the middle of upgrading my WordPress installation, I’ll check the link after the installation process. In meantime you can use this url feed:http://www.rialvalue.com/blog/feed/
I’ll check the problem you mention with fx:Sting and I’ll let you know