Shopify Liquid capture settings not outputting for some reason - html

I'm trying to get and display some theme settings which gets the site name and applies to end which i have setup in the schema but cant get it to display the settings.
In the promo_header.liquid snippet file i have:
{% capture promo_header_text_1 %} locale_promo_1_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_1 %} locale_promo_1_url_{{ shop.name }} {% endcapture %}
{% capture promo_header_text_2 %} locale_promo_2_text_{{ shop.name }} {% endcapture %}
{% capture promo_header_url_2 %} locale_promo_2_url_{{ shop.name }} {% endcapture %}
<p>
{% if settings[promo_header_text_1] %}
{{ settings[promo_header_text_1] }}
{% endif %}
{% if settings[promo_header_text_2] %}
<span>/</span>{{ settings[promo_header_text_2] }}
{% endif %}
</p>
I have also tried to output using just for example
{{ settings.promo_header_text_1 }}
But not displaying anything either... when i debug and display for example
{{ promo_header_text_1 }}
It does return
locale_promo_1_text_website-test2
Which is correct and in the schema settings file (example based on that one above it is matching and is set in theme customisation)
{
"type": "text",
"id": "locale_promo_1_text_website-test2",
"label": "Promo 1 Text",
"default": "FREE US SHIPPING OVER $35"
}
What am I doing wrong?

For anyone else that might come across the same issue here is the solution I got working:
{% assign promo_header_text_1 = 'locale_promo_1_text_' | append:shop.name %}
and to output
{{ settings[promo_header_text_1] }}

Related

Retrieve first image from a Wagtail Streamfield for blog_index_page

Assume I am following the Your first Wagtail site tutorial, except I want to use a Streamfield, instead of separate RichTextField or BlogPageGalleryImage. For example :
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
... becomes ...
class BlogPage(Page):
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('intro', blocks.RichTextBlock()),
('body', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
], null=True, blank=True,)
How can I (or can I) pull the first image and the beginning of intro (...as I cannot restrict length) from a Streamfield for use in the blog_index_page.html template, and avoid using separate fields and BlogPageGalleryImage Class? Therefore replacing this template :
{% for post in blogpages %}
{% with post=post.specific %}
<h2>{{ post.title }}</h2>
{% with post.main_image as main_image %}
{% if main_image %}{% image main_image fill-160x100 %}{% endif %}
{% endwith %}
<p>{{ post.intro }}<p>
{% endwith %}
{% endfor %}
With something more like this :
{% for post in blogpages %}
{% with post=post.specific %}
<h2>{{ post.title }}</h2>
{{ post.body.image.first() }}
<p>{{ post.intro.get_words(50) }}</p>
{{ post.body|richtext }}
{% endwith %}
{% endfor %}
Many thanks.

How to get content from another page in Jekyll?

I want to display some pages of my Jekyll website in my Home page. How can I accomplish this?
Currently I am trying to do the following:
{% assign sorted = (site.pages | sort: 'order_home') %}
{% for my_page in sorted %}
{% if my_page.order_home %}
{{ my_page }}
{% endif %}
{% endfor %}
However, what happens is that content is exhibited in the following form:
{"layout"=>"page", "title"=>"Blog", "permalink"=>"/blog/", "comments"=>false, "order_nav"=>4, "order_home"=>3, "content"=>"
\n{% for category in site.categories reversed %}\n
\n {% capture category_name %}{{ category | first }}{% endcapture %}\n
\n\n
{{ category_name }}
\n \n {% for post in site.categories[category_name] %}\n
\n
{{post.title}}
\n
\n {% endfor %}\n
\n{% endfor %}\n
\n", "dir"=>"/blog/", "name"=>"posts.md", "path"=>"posts.md", "url"=>"/blog/"}
I would like to make the content appear as if it was the rendered page itself, with the {{...}} blocks correctly processed. Any idea on how to do this?
Thanks!
Just add .content to my_page. This results in the following code:
{% assign sorted = (site.pages | sort: 'order_home') %}
{% for my_page in sorted %}
{% if my_page.order_home %}
{{ my_page.content }}
{% endif %}
{% endfor %}

Nested "for" loop inside "if" condition in Liquid/Jekyll

I'm getting syntax error while trying to generate site. Do you have any ideas what can be wrong here?
Error: Liquid syntax error: Unknown tag 'elsif'
{% if page.title == "Tags" %}
{% for tag in site.tags %}
{% elsif page.title == "Categories" %}
{% for tag in site.categories %}
{% endif %}
{{ tag[0] }}
{% endfor %}
You can't start a loop conditionally like that, control blocks must be properly nested. To accomplish what you're trying to do you could do:
{% if page.title == "Tags" %}
{% assign data = site.tags %}
{% elsif page.title == "Categories" %}
{% assign data = site.categories %}
{% endif %}
{% for tag in data %}
{{ tag[0] }}
{% endfor %}
You have got it all wrong. The nested loops does not work this way.
It should start and end inside the same conditional.
if conditional
for loop
endfor
endif
something like this.
So, the correct way to do it should be this
{% if page.title == "Tags" %}
{% for tag in site.tags %}
{{ tag[0] }}
{% endfor %}
{% elsif page.title == "Categories" %}
{% for tag in site.categories %}
{{ tag[0] }}
{% endfor %}
{% endif %}
This code should do exactly what you want correctly but there is a better approach as answered by JJJ

One variable for different collections in Jekyll to use in forloop

I have several collections on my Jekyll site. I've added post navigation to one of the collections displaying a counter on each post page:
{% assign testimonials = site.testimonials %}
{% assign page_order = 1 %}
{% for node in testimonials reversed %}
{% if node.url == page.url %}
{{ page_order }} from {{ forloop.length }}
{% else %}
{% assign page_order = page_order | plus: 1 %}
{% endif %}
{% endfor %}
I would like to make this code work not only for site.testimonials, but for other collections as well. I tried to pass a variable for collections like this:
{% capture label %}{{ page.collection }}{% endcapture %}
{% assign collection = site.collections | where: "label",label | first %}
{% for node in collection reversed %}
{% if node.url == page.url %}
{{ page_order }} from {{ forloop.length }}
{% else %}
{% assign page_order = page_order | plus: 1 %}
{% endif %}
{% endfor %}
But it doesn't work. Is there any way to pass a variable for all collections in Jekyll to use in forloop in post navigation?
When you access collection with site.testimonials, you get collection's documents array.
{{ site.testimonials | inspect }}
# output >> [#<Jekyll::Document ...>, #<Jekyll::Document ...>, ...]
When you access a collection while looping over site.collection, you receive the collection's object :
{% assign collection = site.collections | where: "label",page.collection | first %}
{{ collection | inspect }}
# output >> { "output": true, "label": "collectionLabel",
"docs": [ doc1, docs2, ... ], "files": [],
"directory": "/path/to/collection",
"relative_directory": "_colectionDirectory" }
In your case, you just have to replace :
{% for node in collection reversed %}
By :
{% for node in collection.docs reversed %}

Liquid + Jekyll, "unless" not working inside "for" loop

I'm making JSON given a set of pages. I want to skip any pages without titles, and the last element can not have a comma after it, that's bad JSON. Tried different variations, here is an example:
---
---
[
{% for page in site.pages %}
{% unless page.title %}
{% continue %}
{% else %}
{
"title":"{{ page.title }}",
"content":"{{ page.content | strip_html | strip_newlines }}",
"href":"{{ page.url }}"
}
{% endunless %}
{% unless forloop.last %},{% endunless %}
{% endfor %}
]
The end of the resulting JSON file looks like this:
{
"title":"Test Search",
"content":" ",
"href":"/search.html"
}
,
]
How do I get rid of the trailing comma? Thank you in advance.
I think your problem is that your last loop iteration is one that has no title.
Try to prepend the comma. That way you don't have to look in the future:
{% assign isFirst = true %}
{% for page in site.pages %}
{% unless page.title %}{% continue %}{% endunless %}
{% unless isFirst %},{% endunless %}
{% assign isFirst = false %}
{
"title": {{ page.title | jsonify }},
"content": {{ page.content | strip_html | strip_newlines | jsonify }},
"href": {{ page.url | jsonify }}
}
{% endfor %}
Edit: You should also use the jsonify filter to ensure proper escaping of quotes and other characters.
Try using {% raw %} to escape your non-liquid brackets, this can cause Jekyll to misbehave without throwing any errors.
If that still leads to odd behavior try creating a seperate array like the following example, which is very similar to the case from OP.
{% assign suggestions = "" | split: ',' %}
{%- for item in site[page.collection] -%}
{%- unless item.url == page.url -%}
{% assign suggestions = suggestions | push: item %}
{%- endunless -%}
{%- endfor -%}
<script id="suggest-json" type="application/json">
[
{%- for item in suggestions -%}
{% assign images = item.media | where: "type", "image" %}
{% assign image = images.first %}
{% raw %}
{
{% endraw %}
"url": "{{ site.baseurl }}{{ item.url }}",
"title": {{ item.title | jsonify }},
"subject": {{ item.game | jsonify }},
"image": {{ image.thumbnail | jsonify }},
"alt": {{ image.thumbnail_alt | jsonify }}
{% raw %}
}
{% endraw %}
{%- unless forloop.last -%},{%- endunless -%}
{%- endfor -%}
]
</script>
The problem may be that Jekyll doesn't actually consider the forloop.index as last because it isn't technically. You can avoid this by creating a new array either with a loop or the 'where' tag (loop used above).
The forloop.last should now behave properly, hope this helps.