Re: Delimiters for String Templates (the ultimate Bike Shed!)

I think it would be rather confusing to use {{expr}} for embedding an expression in a string template, given that we use {expr} in attribute value templates, especially as in AVTs double-curlies are used to escape single-curlies.

Michael Kay
Saxonica


> On 22 Sep 2015, at 22:18, Robie, Jonathan <jonathan.robie@emc.com> wrote:
> 
> Thinking about the desire to optimize for creating XML, HTML, JSON, and CSS, I realized that there are a variety of JavaScript libraries designed to do just that:
> 
> http://mustache.github.io/
> http://www.dustjs.com/
> http://handlebarsjs.com/
> https://github.com/Mrdigs/Interpose.js
> 
> Several of these delimit expressions in string content with "{{" and "}}". Suppose we think very much in-the-box, using syntax that others have used for this purpose.  Although "{{" and "}}" can easily occur with nested JSON objects, it's easy enough to either add whitespace ("{ {") or embed the "{{" as a string if you just can't stand using whitespace in JSON objects.
> 
> So I would like to add the following possibility to the list:
> 
> 5. <<< ... >>> and {{ ... }}
> 
> 
> Let me apply this to a standard Mustache example.  In Mustache syntax, 
> this is a template with a conditional section:
> 
> Hello {{name}}
> You have just won {{value}} dollars!
> {{#in_ca}}
> Well, {{taxed_value}} dollars, after taxes.
> {{/in_ca}}
> 
> In Mustache, a JSON object is supplied to provide the values of variables:
> 
> {
>  "name": "Chris",
>  "value": 10000,
>  "taxed_value": 10000 - (10000 * 0.4),
>  "in_ca": true
> }
> 
> When rendered, this results in the following string:
> 
> Hello Chris
> You have just won 10000 dollars!
> Well, 6000.0 dollars, after taxes.
> 
> Assuming that the variable $a has been bound to the object shown above, here is the XQuery string template using Syntax #5:
> 
> <<<Hello {{$a.name}}
> You have just won {{$a.value}} dollars!
> {{ 
>   if ($a.in_ca) 
>   then <<<Well, {{$a.taxed_value}} dollars, after taxes.>>>
>   else ""
> }}>>>
> 
> Here's what it looks like embedded in a function:
> 
> declare function local:prize-message($a) as xs:string
> {
> <<< 
>  <div>
>    <h1>Hello {{$a.name}}</h1>
>    <p>You have just won {{$a.value}} dollars!</p>
>    {{ 
>      if ($a.in_ca) 
>      then <<< <p>Well, {{$a.taxed_value}} dollars, after taxes.</p> >>>
>      else ""
>    }}
>  </div>
>>>> 
> }
> 
> Here's what it looks like if we produce HTML instead of text:
> 
> declare function local:prize-message($a) as xs:string
> {
> <<<
>  <div>
>    <h1>Hello {{$a.name}}</h1>
>    <p>You have just won {{$a.value}} dollars!</p>
>    {{ 
>      if ($a.in_ca) 
>      then <<< <p>Well, {{$a.taxed_value}} dollars, after taxes.</p> >>>
>      else ""
>    }}
>  </div>
>>>> 
> }
> 
> Here's what it looks like if we produce JSON:
> 
> declare function local:prize-message($a) as xs:string
> {
> <<<
> { 
>     "greetings" : "Hello!",
>     "value" : $a.value
>     {{
>      if ($a.in_ca) 
>      then 
> <<<, 
>    "taxed_value", {{ $a.taxed_value }}
>>>> 
>      else ""  
>     }}
> }
>>>> 
> } 
> 
> Let's compare that to the other four syntaxes using the same functions:
> 
> 
> ### 1.  <<[ ... ]>>  and <<{ ... }>>
> 
> declare function local:prize-message($a) as xs:string
> {
> <<[ 
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <<[ <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> ]>>
>      else ""
>    }>
>  </div>
> ]>>
> }
> 
> Here's what it looks like if we produce HTML instead of text:
> 
> declare function local:prize-message($a) as xs:string
> {
> <<[
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <<[ <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> ]>>
>      else ""
>    }>
>  </div>
> ]>>
> }
> 
> Here's what it looks like if we produce JSON:
> 
> declare function local:prize-message($a) as xs:string
> {
> <<[
> { 
>     "greetings" : "Hello!",
>     "value" : $a.value
>     <{
>      if ($a.in_ca) 
>      then 
> <<[, 
>    "taxed_value", <{ $a.taxed_value }>
> ]>>  
>      else ""  
>     }>
> }
> ]>> 
> } 
> 
> 
> ### 2.   <[ ... ]>   and  <{ ... }>
> 
> declare function local:prize-message($a) as xs:string
> {
> <[ 
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <[ <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> ]>
>      else ""
>    }>
>  </div>
> ]>
> }
> 
> Here's what it looks like if we produce HTML instead of text:
> 
> declare function local:prize-message($a) as xs:string
> {
> <[
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <[ <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> ]>
>      else ""
>    }>
>  </div>
> ]>
> }
> 
> Here's what it looks like if we produce JSON:
> 
> declare function local:prize-message($a) as xs:string
> {
> <[
> { 
>     "greetings" : "Hello!",
>     "value" : $a.value
>     <{
>      if ($a.in_ca) 
>      then 
> <[, 
>    "taxed_value", <{ $a.taxed_value }>
> ]>  
>      else ""  
>     }>
> }
> ]> 
> } 
> 
> 
> ### 3.  @@[ ... ]@@  and @@{ ... }@@
> 
> declare function local:prize-message($a) as xs:string
> {
> @@[ 
>  <div>
>    <h1>Hello @@{$a.name}@@</h1>
>    <p>You have just won @@{$a.value}@@ dollars!</p>
>    @@{ 
>      if ($a.in_ca) 
>      then @@[ <p>Well, @@{$a.taxed_value}@@ dollars, after taxes.</p> ]@@
>      else ""
>    }@@
>  </div>
> ]@@
> }
> 
> Here's what it looks like if we produce HTML instead of text:
> 
> declare function local:prize-message($a) as xs:string
> {
> @@[
>  <div>
>    <h1>Hello @@{$a.name}@@</h1>
>    <p>You have just won @@{$a.value}@@ dollars!</p>
>    @@{ 
>      if ($a.in_ca) 
>      then @@[ <p>Well, @@{$a.taxed_value}@@ dollars, after taxes.</p> ]@@
>      else ""
>    }@@
>  </div>
> ]@@
> }
> 
> Here's what it looks like if we produce JSON:
> 
> declare function local:prize-message($a) as xs:string
> {
> @@[
> { 
>     "greetings" : "Hello!",
>     "value" : $a.value
>     @@{
>      if ($a.in_ca) 
>      then 
> @@[, 
>    "taxed_value", @@{ $a.taxed_value }}
> ]@@  
>      else ""  
>     }}
> }
> ]@@ 
> } 
> 
> 
> ### 4.   <[" ... "]> and <{ ... }> 
> 
> declare function local:prize-message($a) as xs:string
> {
> <[" 
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <[" <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> "]>
>      else ""
>    }>
>  </div>
> "]>
> }
> 
> Here's what it looks like if we produce HTML instead of text:
> 
> declare function local:prize-message($a) as xs:string
> {
> <["
>  <div>
>    <h1>Hello <{$a.name}></h1>
>    <p>You have just won <{$a.value}> dollars!</p>
>    <{ 
>      if ($a.in_ca) 
>      then <[" <p>Well, <{$a.taxed_value}> dollars, after taxes.</p> "]>
>      else ""
>    }>
>  </div>
> "]>
> }
> 
> Here's what it looks like if we produce JSON:
> 
> declare function local:prize-message($a) as xs:string
> {
> <["
> { 
>     "greetings" : "Hello!",
>     "value" : $a.value
>     <{
>      if ($a.in_ca) 
>      then 
> <[", 
>    "taxed_value", <{ $a.taxed_value }>
> "]>  
>      else ""  
>     }>
> }
> "]> 
> } 
> 
> 

Received on Wednesday, 23 September 2015 16:23:19 UTC