I want use bellow code to display custom date in my Jekyll site
{% assign m = page.date | date: "%-m" %}
{% case m %}
{% when '1' %}Januar
{% when '2' %}Februar
{% when '3' %}März
{% when '4' %}April
{% when '5' %}Mai
{% when '6' %}Juni
{% when '7' %}Juli
{% when '8' %}August
{% when '9' %}September
{% when '10' %}Oktober
{% when '11' %}November
{% when '12' %}Dezember
{% endcase %}
But I don't now where to put it (I tried in post.html but does not work)
I've made a template for this.
This template translate a date in a specific language. Here it's french but feel free to change month and day arrays.
This template can be used in an enumeration of post/page (eg: the index page) or in a post/page template.
When used in an enumeration, you need to pass the date to process
{% for post in site.posts %}
<li>
<span class="post-date">{% include custom_date_full_fr.html date = post.date %}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
Where used in a page/post template, you just have to include the template, as the page.date will already be available.
{% include custom_date_full_fr.html %}
custom_date_full_fr.html
{% if include.date %}
{% assign processed_date = include.date %}
{% else if page.date %}
{% assign processed_date = page.date %}
{% endif %}
{% comment %}-------- Test if we have a date to process --------{% endcomment %}
{% if processed_date %}
{% assign month = "janvier,février,mars,avril,mai,juin,juillet,août,septembre,octobre,novembre,décembre" | split: "," %}
{% comment %}------ Note : sunday is the first day in this array -------{% endcomment %}
{% assign day = "dimanche,lundi,mardi,mercredi,jeudi,vendredi,samedi" | split: "," %}
{% assign month_index = processed_date | date: "%m" | minus: 1 %}
{%comment%}----------------------------------------------
Here **minus: 0** is a trick to convert day_index from string to integer and then use it as an array index.
----------------------------------------------{%endcomment%}
{% assign day_index = processed_date | date: "%w" | minus: 0 %}
{%comment%}-------- Output the date ----------{%endcomment%}
{{ day[day_index] }} {{ processed_date | date: "%d" }} {{ month[month_index] }} {{ processed_date | date: "%Y" }}
{% endif %}
See here for more info :
Jekyll Date Formatting Examples by Alan W. Smith
Liquid documentation - date filters
Related
I want to show the featured image of the product if it has no variants attributed to it, but if it does, I want to show the selected variant image via the product.selected_or_first_available_variant.image object.
However despite numerous attempts, on product pages with no variants I get the no image thumbnail which looks like this: http://cdn.shopify.com/s/assets/no-image-2048-5e88c1b20e087fb7bbe9a3771824e743c244f437e4f8ba93bbf7b11b53f7824c.gif
These are the three approaches I have attempted below:
{% if product.variants.price < 1 %}
{% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
{% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}
{% if product.variants.price == 0 %}
{% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
{% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}
{% if product.variants.price == 'null' %}
{% assign featured_image = product.featured_image | img_url: 'master' %}
{% else %}
{% assign featured_image = product.selected_or_first_available_variant.image | img_url: 'master' %}
{% endif %}
None of them work. Why? What do I need to do to get value of no variants?
ALSO: I just tried "undefined" - didn't work either.
{% if product.variants.price == 'undefined' %}...
You can simply use .
{% if product.has_only_default_variant %}
You can see it in action here: product-has_only_default_variant
I'm working on custom solution for Shopify store and my problem is that I can't manage how to avoid adding ' / ' after the last matched tag in the case statement.
I've tried to use some if statements with a forloop filters with no result.
The last thing on my mind was finding the last matched tag in the loop that will help us avoiding ' / ' after the last element, but unfortunately I can't manage how to do that.
Expected result: Wool/Nylon/Viscose
Here is the part of the code that compares all tags assigned to the product with the list of tags(clothing materials) required for the output.
Considering that product has such tags as Wool, Nylon and Viscose and others, non material.
Example 1
Actual & Expected Result: WoolNylonViscose
{% for tag in product.tags %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Polyamide' %}
Polyamide
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
{% endfor %}
Example 2
Filter forloop.last was used to define the last element of the loop, but the problem is that material tags(Wool, Nylon, Viscose) can be in the middle of the product tag array. Considering product has 10 tags and material tags are spread among the array we will see next result.
Result: ///Wool/Nylon////Viscose//
{% for tag in product.tags %}
{% if forloop.last == true %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
{% else %}
{% case tag %}
{% when 'Viscose' %}
Viscose
{% when 'Wool' %}
Wool
{% when 'Nylon' %}
Nylon
{% else %}
{% endcase %}
/
{% endif %}
{% endfor %}
I would appreciate if you could please point on my mistakes and suggest me how can i achieve solution.
This should work:
{% capture tag_string %}{% endcapture %}
{% for tag in product.tags %}
{% if tag == 'Viscose' %}{% capture tag_string %}{{ tag_string }}Viscose/{% endcapture %}
{% elsif tag == 'Wool' %}{% capture tag_string %}{{ tag_string }}Wool/{% endcapture %}
{% elsif tag == 'Polyamide' %}{% capture tag_string %}{{ tag_string }}Polyamide/{% endcapture %}
{% elsif tag == 'Nylon' %}{% capture tag_string %}{{ tag_string }}Nylon/{% endcapture %}
{% endif %}
{% endfor %}
{{ tag_string | split: "" | reverse | join: "" | remove_first: "/" | split: "" | reverse }}
More on string filters: https://help.shopify.com/themes/liquid/filters/string-filters
You can try the following:-
{% for tag in product.tags %}
{% case tag %}
{% when 'Viscose' %}
{{ 'Viscose' | append: '/' }}
{% when 'Wool' %}
{{ 'Wool' | append: '/' }}
{% when 'Polyamide' %}
{{ 'Polyamide' | append: '/' }}
{% when 'Nylon' %}
{{ 'Nylon' }}
{% else %}
{% endcase %}
{% endfor %}
I am trying to share a single template in 2 other templates as follows:
_layouts/V2/post.slim
{% include V2/date_wrapper.html date_value=page.date %}
and
_includes/V2/footer/recent_posts.slim
| {% for post in site.posts limit: 5 %}
| <div class="w-bloglist-entry">
| <a class="w-bloglist-entry-link" href="{{ post.url }}">{{ post.title }}</a>
| <span class="w-bloglist-entry-date">
| <i class="fa fa-clock-o"></i>
| {% include V2/date_wrapper.html date_value=post.date %}
| </span>
| </div>
| {% endfor %}
This is the shared template:
_includes/V2/date_wrapper.html
{% assign d = include.date_value | date: "%-d" %}
{{ include.date_value | date: "%B" }}
{% case d %}
{% when '1' or '21' or '31' %}{{ d }}st,
{% when '2' or '22' %}{{ d }}nd,
{% when '3' or '23' %}{{ d }}rd,
{% else %}{{ d }}th,{% endcase %} {{ include.date_value | date: "%Y" }}
If I set this up using the template only in the post it works, however, no matter what I try I can not seem to get this to work in the footer in the for loop. The error is:
Liquid Exception: undefined method `data' for #<Jekyll::SlimPartialTag:0x007ffa549315b0> in _layouts/V2/post.slim
14:47:34 - ERROR - Jekyll build has failed
Thoughts? Is this possible in Jekyll?
In Jekyll, I would like my home page to list the most recent posts grouped by date, like so:
September 6, 2013
Post 1
Post 2
Post 3
September 5, 2013
Post 1
Post 2
Basically, I just want to spit out a date heading when a post in the loop is from a different date from the one previously processed. I've tried to do this by testing if the next post in the for loop matches the date of the last post, and to display a date header only if it doesn't. This is what my Liquid template looks like:
---
layout: default
title: Home Page
---
{% assign thedate = '' %}
{% for post in site.posts %}
{% if thedate != post.date | date: "%m-%d-%Y" %}
<h2>{{ post.date | date: "%A, %B %e, %Y" }}</h2>
{% endif %}
{% assign thedate = post.date | date: "%m-%d-%Y" %}
<h3 class="headline">{{ post.title }}</h3>
{{ post.content }}
<hr>
{% endfor %}
If instead of using post.date | date: "%m-%d-%Y" I instead say simply post.date it works, and posts are grouped together, but only if the posts have the exact same date and time (not just the same day of the month). That's why I add the more specific post.date | date: "%m-%d-%Y".
Any ideas? Thanks so much for our help!!
Found the answer by modifying the archives solution here: http://www.mitsake.net/2012/04/archives-in-jekyll/
Here is the code that works:
layout: default
title: Home Page
---
{% for post in site.posts %}
{% capture day %}{{ post.date | date: '%m%d%Y' }}{% endcapture %}
{% capture nday %}{{ post.next.date | date: '%m%d%Y' }}{% endcapture %}
{% if day != nday %}
<h5 class="date">{{ post.date | date: "%A, %B %e, %Y" }}</h5>
{% endif %}
{{ post.content }}
<hr>
{% endfor %}
These previous solutions are fantastic and elegant way to get around the shortcomings of previous versions of Jekyll but luckily in late 2016, Jekyll added a group_by_exp filter that can do this much more cleanly.
{% assign postsByDay =
site.posts | group_by_exp:"post", "post.date | date: '%A, %B %e, %Y'" %}
{% for day in postsByDay %}
<h1>{{ day.name }}</h1>
<ul>
{% for post in day.items %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
Documentation can be found on the Jekyll Templates page.
Alternative solution:
Directly capture the date in the format that you want to display at the end.
(here: %A, %B %d, %Y --> Monday, April 30, 2012)
Then you don't need to use | date: that often:
{% for post in site.posts %}
{% capture currentdate %}{{post.date | date: "%A, %B %d, %Y"}}{% endcapture %}
{% if currentdate != thedate %}
<h2>{{ currentdate }}</h2>
{% capture thedate %}{{currentdate}}{% endcapture %}
{% endif %}
<h3>{{ post.title }}</h3>
{% endfor %}
The generated HTML:
<h2>Monday, April 30, 2012</h2>
<h3>Foo</h3>
<h2>Friday, March 09, 2012</h2>
<h3>Bar</h3>
<h3>Baz</h3>
I'm trying to figure out how to display the total number of posts in a category (or all together). I envision something like below, but can't quite figure it out. Did I miss something from the docs?
{% for post in site.categories.CAT %}
...do some counting
{% endfor %}
posts: {% number_of_posts %}
{% for post in site.categories.CAT %}
{{ post.title }}
{% endfor %}
# all posts
{{ site.posts | size }}
# posts in one category
{{ site.categories.CAT | size }}
{% for post in site.categories.CAT %}
{% capture post_count %} {{ post_count | plus: 1 }} {% endcapture %}
{% endfor %}
{{ post_count }}