Liquid Transformation: Replace / with - replace

I have some trouble by replacing a substring with liquid transformation.
This is my output where I want to replace the forward slash by a dot.
"ProductName": "2x19/ display in control room"
My code looks like this:
[
{% for Order_var in content[0].ListOfSmsOrderEntrySales.OrderEntry-Orders.ListOfOrderEntry-LineItems.OrderEntry-LineItems %}
{
"Id":"{{Order_var.Id}}",
"PartNumber":"{{Order_var.PartNumber}}",
"Product":"{{Order_var.Product}}",
"ProductDefTypeCode":"{{Order_var.ProductDefTypeCode}}",
"ProductId":"{{Order_var.ProductId}}",
"ProductIntegrationId":"{{Order_var.ProductIntegrationId}}",
"NetPrice":"{{Order_var.NetPrice}}",
"ProductName":"{{Order_var.Description2 | replace: '/', '.' }}",
},
{% endfor %}
]
I still get the same ouput with no replacement. Any idea? Would be grateful
Cheers

Please use Replace but not replace in your liquid template. The document of liquid shows {{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}, but according to test, we need to use upper case Replace in liquid template.

Related

Regex for matching a tag in a Liquid template : ">" inside html tag

I have to write a match pattern for the body tag in a Liquid template.
While matching HTML tags is pretty straightforward, I have the problem that HTML special character can be used inside the Liquid code.
Example:
<body class="template-{{ template | replace: '.', ' ' | truncatewords:
1, '' }}{% if promo %}has-promo{% endif %} {% if products.size > 1
%}has-related-products{% endif %} {% if settings.product-hover ==
'quick-shop' %}has-quick-shop{% endif %} loading" >
or simplified:
<body {% bla > 1 %} bla bla>
My current match pattern /<body(.[^>]*)>/s matches the above code until the first >. I need a pattern that matches the whole tag.
Try with:
/<body(.[^>{}]*(?:{+[^}]*}+[^>{}]*)*)>/s
See demo
Instead of [^>]* the regex uses [^>{}]*(?:{+[^}]*}+[^>{}]*)*, that matches any character but >, { or }; at some point it can meet a {, so it matches the whole content of {+something}+, followed again by [^>{}]*. This trick is repeated many times with the last *.

Balanced regular expression

so I got my hands on regular expressions and tried to match the outer {% tag xyz %}{% endtag %} tags of the following text using regular expressions:
{% tag xyz %}
{% tag abc %}
{% endtag %}
{% endtag %}
My regular expression looks as follows and works so far:
({%)\s*(tag)([^%}]*?)(?:\s*(?:(%})((?:(?:[^{%]*?)|(?R))*)(?:({%)\s*(end\2)\s*(%}))))
But whenever the text inside of the matching tags contains a single { or % sign, the regex won't work as expected. I think it's because of the character classes that may match {% but also { or % as single characters too. I tried a lot and ended up with try and error but without success.
Any help on that issue?
I setup two regex101 links for you to show the issue:
works: https://regex101.com/r/qH0rI5/1
does not work: https://regex101.com/r/qH0rI5/2
Any help is really appreciated!
Try to to replace [^{%] with (?:(?!{%).) and add the s (PCRE_DOTALL) flag:
This would allow { that are not followed by % in between by using a negative lookahead.
Test your updated pattern or here another start to try with:
/{% tag \w+ %}(?:(?:(?!{%).)|(?0))*{% endtag %}/gs
test at regex101

Find every string between quotes inside specific curly braces

I'm working on a script helping me catching all my localized strings in my project and I'm stuck on a RegEx.
In the following string {{ translateAttr title="button.add_user.title" data-disable-with="button.add_user.disabled" }} I'd like to be able to catch "button.add_user.title" and "button.add_user.disabled" because my curly braces starts with translateAttr attribute.
So far, I've come with this rule \{{2}translateAttr .*=(['"])(.+?)(?=(?<!\\)\1)\1 ?}{2} but as you could see here http://lumadis.be/regex/test_regex.php?id=2362 it is not matching all the occurrences.
A little help here would be much appreciated.
EDIT: Patterns I'd like the regex to match too
{{ translateAttr title="button with \"escaped quotes\" here" data-disable-with='button with single quotes' }}
{{ translateAttr title="want this with ' single quote" "but not this one" }}
EDIT2 : Patterns I don't like to match
{{ title="because no translateAttr" }}
{{ translateAttr "because no something= attribute before" }}
Thanks
Use the below regex which uses the PCRE verb (*SKIP)(*F)
^(?!.*?\{{2}\h*translateAttr).*(*SKIP)(*F)|=(["'])((?:\\\1|(?!\1).)*)\1
DEMO
Explanation

Simple TextWrangler Grep Replace

I am trying to replace all h2 tags containing the class "title" with h1:
<h2 class="title">Things</h2>
I'm using the Multi-File search in TextWranger with this:
<h2 class="title">[^>]*</h2>
I'm able to find everything, but when I hit replace, it replaces my titles with the grep crap.
BEFORE: <h2 class="title">Things</h2>
AFTER: <h1 class="title">[^>]*</h1>
My problem is that the search not only replaces my tags but also replaces my content with [^>]*. I also tried this in Aptana and the same thing happened. I would appreciate some insight.
It looks like you're converting <h2 class="title"> to h1? You have to use a backreference in your "replace":
Search: <h2 class="title">([^>]*)</h2>
Replace: <h1 class="title">\1</h1> (aside- if it's a h1 do you still want to preserve the 'class="title"'?)
Note the brackets in the search regex, which save what's inside them.
Then you use \1 to pull them back out in the replace text (\1 for the first set of brackets, \2 for the second, ... )

Outputting Literal curly braces in Liquid templates

I'm trying to output the following from within a liquid template:
{{ example }}
Obviously, Liquid sees this as a variable named example and tries to do substitution. I'm trying to find out how I can output the actual braces.
So far, I've found one method that works, but it's incredibly ugly:
{{ '{example'|prepend:'{' }}}}
Yeah, told you it was gross.
Here are other things I've tried:
{{{ example }}} # outputs '}'
{{{{ example }}}} # outputs '}}'
\{\{ example \}\} # outputs '\{\{ example \}\}'
Any advice here?
You can also use raw:
{% raw %}
...lots of liquid code goes here and it doesn't get interpreted...
{% endraw %}
What about using the numeric HTML entities { and } for { and } respectively - presumably this is to be output as HTML?
EDIT: Forgive me, I'm not too familiar with liquid (so this might be very wrong), but can you assign your {{ example }} special value to a variable and output that? May be something like:
{% assign special = '{{ example }}' %}
{{ special }}
This is the only thing that worked from me. Lifted from here:
{{ "{{ this " }}}}
I needed this because I wanted to reference the site global variable from inside a mustache template.
I wanted to have both curly brackets AND angle brackets when formatting a fenced code block, so I ended up with the following pattern:
{% capture code %}{% raw %}line 1
line 2
line 3
{% endraw %}{% endcapture %}
<pre><code>{{ code | replace: "<", "<" | replace: ">", ">" }}</code></pre>
You can escape the HTML, for example in a {{var}} you can use \{\{var\}\}, so that way luquid don't process it.