If statement syntax with Squarespace JSON templates

1.4k views Asked by At

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?

2

There are 2 answers

3
meda On

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}
0
Brandon On

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.