jsq — return a string for use in JavaScript, quoted and with variables substituted
| Attribute | Pos. | Req. | Default | Description |
|---|---|---|---|---|
| interpolate | 0 | interpolate input? | ||
| reparse | 0 | interpolate output? | ||
| hide | 0 | Hide the tag return value? |
[jsq] tag quotes (escapes) strings and performs basic variable
substitution, for use in JavaScript code blocks.
This is mostly used for long strings which are hard to prepare manually.
Example: Basic example
Here's an example of JavaScript code and the output it would generate, once expanded by Interchange:
<script> var astring = 'just an insert'; var somevar = [jsq] Big long string you don't want to have to quote for JS, and you want to insert the variable $astring.[/jsq]; </script>
Expands to:
<script> var astring = 'just an insert'; var somevar = " Big long string you don't" + ' want to have to quote for JS, and you want to' + ' insert the variable ' + astring + '.'; </script>
Interchange 5.9.0:
Source: code/UI_Tag/jsq.coretag
Lines: 31
# Copyright 2002-2007 Interchange Development Group and others
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. See the LICENSE file for details.
#
# $Id: jsq.coretag,v 1.8 2007-03-30 23:40:54 pajamian Exp $
UserTag jsquote Alias jsq
UserTag jsq hasEndTag
UserTag jsq NoReparse
UserTag jsq PosNumber 0
UserTag jsq Version $Revision: 1.8 $
UserTag jsq Routine <<EOR
sub {
my $text = shift;
$text =~ s/^[ \t\r]*\n//;
my @lines = split /\r?\n/, $text;
for(@lines) {
( $_ !~ /'/ and s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1' + $2 + '/g, $_ = qq{'$_'} )
or
( $_ !~ /"/ and s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1" + $2 + "/g, $_ = qq{"$_"} )
or
( s/'/\\'/g, s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1' + $2 + '/g, $_ = qq{'$_'} );
}
@lines = "''" unless @lines;
return join (" +\n", @lines);
}
EOR