Question

I'm try to emit a literal dollar sign, opening curly bracket, text, and closing curly bracket using the Spark view engine. How can I get get Spark to emit ${Hello}, rather than trying to evaluate a variable name Hello? The best that I can come up with is ${'$'}{Hello}, but that seems too complicated and it's hard to read.

For context, I'm using Spark as an ASP.NET MVC view engine, but I'm also using it as a templating engine, so (an advanced) user of my application can type a simple Spark view into a text area and save it render e-mails, etc.

Thanks!

Was it helpful?

Solution

There are two ways you can escape the Spark code expressions:

  1. Single item escaping
  2. Entire block escaping

Single Item escaping

This can be done by using one of three escape chars for each expression type. Rather than explain, the following is an example of how you would do this in your spark view:

<div>
    $${Encoded.Escaped.with.a.dollar < 0}
    \${Encoded.Escaped.with.a.backslash < 0}
    `${Encoded.Escaped.with.a.backtick < 0}
</div>
<div>
    !!{Unencoded.Escaped.with.a.dollar < 0}
    \!{Unencoded.Escaped.with.a.backslash < 0}
    `!{Unencoded.Escaped.with.a.backtick < 0}
</div>
<div>
    $$!{Encoded.Silent.Nulls.Escaped.with.a.dollar < 0}
    \$!{Encoded.Silent.Nulls.Escaped.with.a.backslash < 0}
    `$!{Encoded.Silent.Nulls.Escaped.with.a.backtick < 0}
</div>

results in the following verbatim output:

<div>
    ${Encoded.Escaped.with.a.dollar < 0}
    ${Encoded.Escaped.with.a.backslash < 0}
    ${Encoded.Escaped.with.a.backtick < 0}
</div>
<div>
    !{Unencoded.Escaped.with.a.dollar < 0}
    !{Unencoded.Escaped.with.a.backslash < 0}
    !{Unencoded.Escaped.with.a.backtick < 0}
</div>
<div>
    $!{Encoded.Silent.Nulls.Escaped.with.a.dollar < 0}
    $!{Encoded.Silent.Nulls.Escaped.with.a.backslash < 0}
    $!{Encoded.Silent.Nulls.Escaped.with.a.backtick < 0}
</div>

Entire block escaping

You can use the new <ignore> special tag to output verbatim anything that is inside a block like so:

<html>
  <head>
    <title>ignore test</title>
  </head>
  <body>
    <h1>ignore test</h1>
    <p>${System.DateTime.Now}</p>
    <ignore>
        <div>
            Regular text ${This.isnt.code < 0}
            <var dummy="This isn't a variable" />
        </div>
    </ignore>
  </body>
</html>

Hope that helps,
Rob

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top