Checking a string with an ampersand - a bug in Shopify?

Shopify Partner
5 0 0

Hi,

I have a string that looks like "some name & some other".

This is being returned in my

{{ line_item.title }}

However I'm noticing that the appersand ( the & ) is causing some issues - namely if I use the following in a if statement it's not entering my if condition:

So:

{% if line_item.title contains "some name & some other"  %}     
   DO SOMETHING             
{% endif %}

If however I remove that appersand, it goes into the if condition but then I loose what I am trying to achieve.

I attempted to use the backslash (escape) however it didnt work - namely

{% if line_item.title contains "some name \& some other"  %}     
   DO SOMETHING             
{% endif %}

Any ideas/help would be appreciated - wondering if this an issue in liquid or simply on my side.

Thanks.

0 Likes
Highlighted
Navigator
1151 0 118

I tried to reproduce just using an ordinary variable and could not. Here is what I used.

{% assign mystring = "xxxx a & b xxxx" %}
mystring = {{ mystring }}<br>
{% if mystring contains "a & b" %}worked as expected - a & b found{% else %}a & b not found{% endif %}

How about 

{% assign myLineTitle = line_item.title %}
{% if myLineTitle contains "some name & some other"  %}     
   DO SOMETHING             
{% endif %}

 

http://www.StandoutDesigns.com ::: Solid Wood TV Furniture for Enthusiasts. Made in USA.
0 Likes
Shopify Partner
5 0 0

So I tried that method of assigning the string however it doesnt work on my side.

Your code however did work (also as I'd expect).

This leads me to believe there is something fishy with the line_item.title.

Infact I tried the string filter strip however that did nothing - infact it went into the if statement for every string.

{% if line_item.title | strip contains "some name & some other"  %}     
   GOES HERE FOR EVERY line_item            
{% endif %}

I'm really quite stumpped here because if I do a generic {{ line_item.title }} I see the string printed as I'd expect. Although looking at the code I do see a lot of white space and new lines before and after the string.

 

0 Likes
Navigator
1151 0 118
{% assign myLineItemTitle = line_item.title | replace "&", "^" %}
{% if myLineItemTitle contains .... %}{% endif %}

If indeed the ampersand is causing the issue, how about assigning the line_item.title to a variable, replacing the "&" with something else, like "^", and then doing your IF on the variable where the other side of the comparison also uses a variable with "^" replacing "&"?

http://www.StandoutDesigns.com ::: Solid Wood TV Furniture for Enthusiasts. Made in USA.
0 Likes
Shopify Partner
5 0 0

Thanks - so I'e tried that replace

{% assign a = "j @j.3 & m @p 動画 事前予約" %}
{% assign b = a | replace: "&", "^" %}

{{ b }}

however I'm then returned a string with the text 'amp;' so:

j @j.3 amp; m @p 動画 事前予約

Why is this?

Thanks

0 Likes
Shopify Expert
9965 84 1464

I tried your example and the replace worked fine. Is it possible that the character you're adding is not actually a true ampersand character - but just looks like one?

Let's get a link to the product page so we can see json code (by adding .json to the end of the url). That will help to make sure we're all talking and testing on the exact some characters.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Partner
4 0 0
{% assign b = a | replace: "&", "^" %}
if you do in this way it will work
{% assign b = a | replace: "&amp;", "^" %}

because in this  string it is being treated as a html ampersand 
0 Likes
Shopify Partner
4 0 0
{% assign b = a | replace: "&", "^" %}

 if you do in this way it will work
{% assign b = a | replace: "&amp;", "^" %}

because in this  string it is being treated as a html ampersand 

0 Likes
Shopify Partner
4 0 0

{% assign b = a | replace: "&", "^" %}

 if you do in this way it will work
{% assign b = a | replace: "&amp;", "^" %}

because in this  string it is being treated as a html ampersand 

0 Likes