Blog Entry - 24th July 2007 - Website - News

Code Play With Added Data

One practical limitation for my CodePlay idea is supplying certain kinds of String literal data to the code examples.

Particularly data which:-

  • is multi-line: so it cannot be conveniently represented in JavaScript without using \r and \n escapes; or
  • contains mark-up characters, such as < and >, which may need to be escaped if it were stored in an HTML tag such as a pre.

I could provide a separate input or 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 /* ... comment ... */ structure in JavaScript to create data regions, which are then pre-processed.

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:-

Some Data
Some-more Data

My preprocessor will then make it available to your code in a supplied data variable.

The first and last line breaks are trimmed.



Some data
Some data

Additional data
Additional data


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. 


So I have created an alternative, which again pre-processes the code and converts any heredoc syntax it finds into escaped JavaScript string literals.

Here is an example:-

var someVariable = <<<ZZZ

Some text
	And More
		And More





The /*region1 ... region1*/ version is available for CodePlay.

The <<< version is available for CodePlay and PagePlay.


Sorry, comments have been suspended. Too much offensive comment spam is causing the site to be blocked by firewalls (which ironically therefore defeats the point of posting spam in the first place!). I don't get that many comments anyway, so I am going to look at a better way of managing the comment spam before reinstating the comments.

Leave a comment ...

{{PREVIEW}} Comments stopped temporarily due to attack from comment spammers.