One practical limitation for my CodePlay idea is supplying certain kinds of
String literal data to the code examples.
Particularly data which:-
- contains mark-up characters, such as
>, which may need to be escaped if it were stored in an HTML tag such as a
I could provide a separate
text area for the data.
However, my solution is to carry out an element of pre-processing to the code, before it is
eval'd; so, I have come up with two options.
Option 1 - Make use of the multi-line comment structure.
Here I make use of the
It is not possible to use this without pre-processing, because in normal code, a number of browsers strip out the comment text from the value returned by Function.prototype.toString().
Anyway, the basic idea is to store your data as follows:-
/*NAME Some Data Some-more Data NAME*/
My preprocessor will then make it available to your code in a supplied
The first and last line breaks are trimmed.
alert(data.region1); alert(data.region2); /*region1 Some data Some data region1*/ /*region2 Additional data Additional data region2*/
Option 2 - PHP Heredoc
However I would love to have something akin to the
heredoc syntax in PHP:-
Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example. Closing delimiter (possibly followed by a semicolon) must be followed by a newline too.
If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script.
Here is a PHP example:-
$myMultiLineString = <<<IDENTIFIER Some data Some-more data. IDENTIFIER;
Here is an example:-
var someVariable = <<<ZZZ Some text And More And More ZZZ; alert(someVariable);
/*region1 ... region1*/ version is available for
<<< version is available for