When I try to sort post there an Expected end_of_string but found pipe error - jekyll-theme

I'd like to sort the post in the reverse sequence, but there was an error.
I had tried to remove | , however, it not work well.
callback([
{% for post in site.posts reversed | sort: title %}
{
"text": "{{post.title | replace:'"','\"'}}",
"href": "{{site.url}}{{site.baseurl}}{{post.url}}"
} {% unless forloop.last %},{% endunless%}
{% endfor %}
])
There are Expected end_of_string but found pipe errors.
Liquid Warning: Liquid syntax error (line 2): Expected end_of_string but found pipe in "post in site.posts reversed | sort: title" in proselinks.jsonp

Related

Django template syntax error: could not parse remainder % 2

I am getting a TemplateSyntaxError: "could not parse remainder % 2 from num%2":
{% if num%2 ==0 %}
{{"Even"}}
{% else %}
{{"Odd"}}
{% endif %}
You can't use arbitrary Python expressions in Django templates. You should create a custom filter for them.
However, for your expression there is a built-in tag divisibleby. From its example:
{{ value|divisibleby:"2" }}
If value is 4, the output would be True. So the final answer looks like (untested):
{% if num|divisibleby:"2" %}
Even
{% else %}
Odd
{% endif %}

How to access key name in json_string type of Shopify?

For example I have this JSON string in product_supplier['shipping_to']:
{"United States": {"time": "1-3"}, "Worldwide": {"time": "10-15"}}
How can I access the country name in liquid? Tried this:
{% for country, time in product_supplier['shipping_to'] %}
{{ country }}: {{ time['time'] }}
{% endfor %}
Which obviously doesn't work as it gives error: Liquid syntax error: Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]
And tried this:
{% for country in product_supplier['shipping_to'] %}
{{ country[0] }}, {{ country[1] }}
{% endfor %}
Which gives empty output with just a comma:
,
From the official doc here, it seems it's only able to access the values not keys?
If you saved the metafield as json_string then you can do this.
{% for item in product.metafields.product_supplier.shipping_to %}
{{item[0]}}<br/>
{% endfor %}
The response will be:
United States
Worldwide
Once again this must be as json string to work.
So it similar to your third example but we are using the proper object (in this case product.metafields) to target that metafield.
Tested it on my end it works without a problem.

Shopify (Liquid): Syntax error expected close_square but found comma

I am creating an array in Shopify (Liquid) and I get an error,
{% assign numbers = [
"One",
"TWo",
"three",
"bla"
]
%}
Line 126 — Liquid syntax error: Expected close_square but found comma
in "{{[ "One","TWo", "three","bla" ] }}"
There is no way to create an array like this in liquid.
Instead, you can use the split filter to create an array from a string.
{% assign numbers = "one,two,three,four" | split: "," %}
<pre>{{ numbers | inspect }}</pre>
You can also create an empty array and feed it with the push filter
{% comment %} +++ Creates an empty array +++ {% endcomment %}
{% assign numbers = "" | split: "" %}
<pre>{{ numbers | inspect }}</pre>
{% comment %} +++ Feed the beast +++ {% endcomment %}
{% assign numbers = numbers | push: "one" %}
<pre>{{ numbers | inspect }}</pre>
{% assign numbers = numbers | push: "two" %}
<pre>{{ numbers | inspect }}</pre>

Liquid Warning: Unexpected character

To assign class depending on page category I have following in my code:
{% assign category_class = 'category-' | append: {{ page.category }} %}
As expected I get <div class="category-sometext". But when building I also get warning about an unexpected character in this line.
What's wrong and how I can fix it?
You need to remove the {{ }} around page.category as you are already inside {% %}. So:
{% assign category_class = 'category-' | append: page.category %}

Using a string to create a Liquid variable

In my shopify store, I am using SuperFields in order to customize my site, though my question isn't about the app. On one of my pages, I need the value for the following:
variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]
The value should be 0 or 1. If I evaluate the statement directly, such as:
{if variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key] =1%}
It throws an error when I render the page: Unexpected character '{'
I've also tried the following:
{% capture defaultImage %}variant.metafields.sf_{{ collection.title | downcase }}[meta_tag_key]{% endcapture %}
{% assign test = defaultImage %}
But 'test' is considered nil and doesn't return any value. I have tried to search for answers here and on the shopify forum, but, as my clumsy post title suggests, I'm having a hard time concisely searching for a solution to this problem. Any help is greatly appreciated.
You can try :
{% assign metafield-key = collection.title | downcase | prepend: "sf_" %}
{% assign key = variant.metafields[metafield-key][meta_tag_key] %}
{% if key == 1 %}
Do the twist !
{% endif %}
You are missing a % sign in your code. Hence the error message. Your if statement started with {% and not just {
If you working in liquid then you have to use {% %} for defining any variable & also for condition in shopify. You can't use { this.