Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping Characters in Liquid String

I am trying to put together a tag list that contains various sizes (in Shopify using Liquid). The sizes use single and double quotes for inches and feet. Because it uses both, it is causing issues with the string closing properly. I have tried using a standard escape character '\', but that doesn't seem to work. Is it possible to escape characters in Liquid or is there another method someone can recommend?

{% assign tags = "4'x6', 5'x8', 8'x10', 9'x12', 10'x14', 5'x7', 3'x5', 2'x3', 6'x9', 16\"x16\"x16\", 24\"x36\", 20\"x32\", 20\"x48\", 20\"x72\", 42\"x48\" rectangular, 55\"x57\" with lip" | split: ',' %}
like image 775
Andrew Avatar asked Oct 07 '15 03:10

Andrew


2 Answers

In liquid you can escape them with \' \" etc but your application will probably escape the whole sequence. Instead use unicode:

{% assign tags = "4\2019 × 6\2019, 5\2019 × 8\2019, ..., 16\201D × 16\201D × 16\201D, 24\201D × 36\201D, ..., rectangular, 55\201D × 57\201D with lip" | split: ',' %}

For reference http://www.blooberry.com/indexdot/html/tagpages/entities/genpunctuation2.htm

I typically construct my whole template and then do replacements at the end:

{% capture escape_to_unicode %}{% assign tags = "4′ × 6′, 5′ × 8′, ..., 16″ × 16″ × 16″, 24″ × 36″, ..., rectangular, 55″ × 57″ with lip" | split: ',' %}{% endcapture %}{{ escape_to_unicode | replace: "‘", \2018 | replace: "’", \2019 | replace: '“', \201C | replace: '”', \201D }}

to boot: I'm pretty sick of people answering questions about escaping characters with typographical opinions. The reality is that there are contexts where you must use the "non-human" equivalent for humans. My example is typically in email html/css, I'm sure there must be other scenarios.

like image 154
Daniel Antonin Black Avatar answered Oct 06 '22 20:10

Daniel Antonin Black


Typographically speaking, quotation marks are the wrong glyph to use to indicate feet and inches. Instead, you should use the prime () and double prime () symbols, respectively, and the multiplication sign (×) instead of "x":

{% assign tags = "4′ × 6′, 5′ × 8′, ..., 16″ × 16″ × 16″, 24″ × 36″, ..., rectangular, 55″ × 57″ with lip" | split: ',' %}

If you're set on using quotation marks, perhaps you can use HTML entities (I'm not sure if this works or not):

{% assign tags = "4'x6', ..., 16"x16"x16", 24"x36", 20"x32", ..." | split: ',' %}
like image 40
Jordan Running Avatar answered Oct 06 '22 18:10

Jordan Running