0

I am trying to evaluate a string in if statement using JSON templates.

This works:

{.if true}
  <h1>It is true</h1>
{.end}

However, this syntax breaks the page:

{.if "foo"=="bar"}
  <h1>Not true</h1>
{.end}

How do I compare strings using JSON templates?

SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84

2 Answers2

3

Squarespace solves this problem using the ".equal" predicate (I can't post the link due to not enough rep. points):

{.equal? foo "bar"}
  <h1>It's true</h1>
{.or}
  <h1>Not true</h1>
{.end}

where foo is the key to a value provided within the JSON data. If you only want the "false/not-equal" part of it, then it could be condensed to:

{.equal? foo "bar"}{.or}<h1>Not true</h1>{.end}

Now, in cases where "bar" contains a space, you'll need to use a different character than a space as a delimiter. From the Squarespace Forums:

The solution then is to specify any other character as a delimiter (one that won't be used in any other part of the string). For example, this will work:

{.equal?:categoryFilter:"Family Law"}

You can see that instead of using a space after .equal?, I've used a : (colon). That way, the space in your second argument "Family Law" is treated correctly and not attempting to delimit the words into two separate arguments.

Note: to see that data that is provided for a page, add "?format=json-pretty" to the url. Here's an example.

Brandon
  • 3,572
  • 2
  • 12
  • 27
0

I don't think this is doable, and I'm sure you don't wanna harcode values like this especially on a template that defeats the purpose.

try:

{.if foo}
  <h1>Not true</h1>
{.end}
meda
  • 45,103
  • 14
  • 92
  • 122