DaveW
DaveW

Reputation: 61

How do I check for quotation marks in liquid?

I am working with an input string like 'James "Jim" Smith' with a person's nickname being double quoted within a string. I have been trying to find if a person has a nickname by using the contains operator, but it never finds entries with double quotes. As an example:

<-- Name - James "Jim" Smith -->
{% if Name contains '\"' %}
   Do Something
{% else %}
   Always gets here
{% endif %}

How can I search for the literal double quote in a string using contains (or split)?

Upvotes: 6

Views: 11830

Answers (7)

Ben
Ben

Reputation: 1

Did some testing and it can be done, although it's not pleasant.

{% if stringVarWithQuote contains '"' %}
  <p>Never true</p>
{% endif %}

{% if stringVarWithQuote contains '\"' %}
  <p>Never true</p>
{% endif %}

{% if stringVarWithQuote contains """ %}
  <p>Always true (even without a quote character in the string)</p>
{% endif %}

{% if stringVarWithQuote contains """" %}
  <p>Always true (even without a quote character in the string)</p>
{% endif %}

{% if stringVarWithQuote | url_escape contains '%20' %}
  <p>Always true (even without a quote character in the string)</p>
{% endif %}

{% assign stringVarWithQuoteCompare = stringVarWithQuote | remove: '&quot;' %} 
{% if stringVarWithQuote != stringVarWithQuoteCompare  %}
      <p>This works!</p>
    {% endif %}

From my testing no comparison to a string literal will work. Often the comparison will always be true, which is easy to overlook in testing. The only way I could get this to work was to create a second variable using the 'remove' filter to strip out quote characters, then compare the two variables. I am unimpressed with this language.

Upvotes: 0

Jin William
Jin William

Reputation: 41

My approach to solve the problem:

{{ post.title | remove: '&quot;'}}

Upvotes: 3

Alexander Bardis
Alexander Bardis

Reputation: 1

Remove: '"' seems working to me

Upvotes: -2

Dawid Dahl
Dawid Dahl

Reputation: 457

If none of the above works for you, try this. It did it for me when removing double quotes:

| remove '"'

Upvotes: 0

Amit Chaudhary
Amit Chaudhary

Reputation: 111

With Liquid map I have observed \\\" translates into \"

Upvotes: 1

Wesely
Wesely

Reputation: 50

I have read about escaping both single and double quotes by doubling them and had some success myself doing this...

In my own example I was trying to remove quotes from a string And the following did NOT work:

{{ post.title | remove: """ }}

However this DID work:

{{ post.title | remove: """" }}

I think that would work in your if statement:

{% if name contains '""' %}

But am not sure how that would work where you are assigning the string or if you need to escape it in your "assign"

Upvotes: 0

GeorgeButter
GeorgeButter

Reputation: 2589

You don't need to escape it. The following will return true:

{% assign name = 'James "Jim" Smith' %}
{% if name contains '"' %}
  true
{% else %}
  false
{% endif %}

Upvotes: 3

Related Questions