I am fairly new to the coding language and what I am trying todo is probably fairly difficult but I would like to attempt it if I can.
I have dispatch notes which I would like to display an image if it is express delivery, so it is easily noticeable. The dispatch notes are in html/ liquid. This is where I have got to so far but it doesn't seem to work
{{% if contains_Standard UK %}} {{ 'express.png'> | | img_tag }}
it is for the information that comes from this {{ order.shipping_lines[0].title }}
First, double {{ is for displaying something. To execute something, you must start with {%.
Then, as any language, when you want to check a condition, it is necessry to compare two values.
Here, you should have something like:
{% if order.shipping_lines[0].title contains 'Standard UK' %}
Do something
{% endif %}
HTH
Related
I guess the most basic question I have is why does using a number at the beginning of a Liquid if statement seem not to work.
This works:
{% if quip.show == "foo" %}
This does not:
{% if quip.show == "1" %}
…but of course there's a lot more to it, here's what I have going on.
I have a Jekyll Collection of little quips I want to show when the site builds on a specific date. A daily status type thing. I setup a collection with the files, made my template, things are marvelous. The issue is making it so only one shows up at a time on the assigned date.
My first thought was to add date: 2020-06-17 to the front matter then run a check to see if that matches today's date but that seems to be conflicting with the Jekyll implied date. So I changed the front matter tag to show: 2020-06-17 just to troubleshoot.
So now I have a for loop that shows all of my quips like this:
{% for quip in site.quips %}
(this is where my HTML is and it loops and renders each collection file perfectly)
{% if quip.show == "2020-06-17" %}
It's working. (this is where the quip HTML will go but I'm isolating it)
{% endif %}
{% endfor %}
That's all manual, the front matter literally says show: 2020-06-17 but obviously I'd like it to automatically get the date, more on that in a bit. …but it doesn't print "It's working.". Oddly, when I change the front matter to show: foo in one of my collection files it does show "It's working". Also, when I change it to just the number 1 it doesn't work. So it seems like Jekyll doesn't like numbers as the first character in a ``==`.
So basically this is what I would like to do:
I have a variable set using this:
{% capture my_var %}{{ "now" | date: "%Y-%m-%d"}}{% endcapture %}
I added this at the top of my page to grab the date (after some other things didn't seem to work)
…this outputs cleanly to 2020-06-17 (as confirmed by calling the variable and looking at the HTML output.)
So then I'd like to put this in my collection loop:
{% if quip.show == "{{ my_var }}" %}
It's Working
{% endif %}
(the quotes around {{ my var }} are tripping me up a bit because the compiler complains if they aren't there, but things don't reliably work without them. No clue there, not the main issue but one that I need to solve).
I mean, the idea is to wrap all of my quip template with this date detector if statement instead of printing "It's working" but that would be a nice proof of concept.
I'm really not opposed to using Jekyll's built in date frontmatter or even the filename -- there's only one per day. I've tried so many things and for some reason I just can't get this to work when I'm using a numeric value in the == statement.
Any help would be much appreciated.
This is happening because the front matter is YAML, and YAML might be more clever than you thought it would.
date: 2020-06-17
Is an actual valid date for YAML.
So if I JSON print such a date defined in a front matter, then, it would show as a date:
{{ quip.date | json }}
2020-06-17 00:00:00 +0200
So if you want to compare dates from the front matter and from the day, you'll have to format both dates.
{% assign now = "now" | date: "%Y-%m-%d" %}
{% assign show = quip.show | date: "%Y-%m-%d" %}
{% if show == now %}
<p class="fresh-from-the-day">quip.title</p>
{% endif %}
Hi I'm new to Jekyll and have a basic blog set up. Now my idea is to loop through a collection per blog post to get some fancy HTML going on. Because as far as I'm aware you can't simply add HTML to the markdown.
// config.yml
collections:
- hollowknight
So I've set up a collection named hollowknight and I got a blog post about Hollow Knight.
In the Front Matter of the blog post I have collectionid: 'hollowknight'. And in the Layout I use for blog posts I tried the following:
// post.html
{% capture collection %}site.{{ page.collectionid }}{% endcapture %}
{% for section in collection %}
{{ section.title }}
{% endfor %}
So the idea was to set the collection variable to site.hollowknight as configured in the config and the Front Matter. I set up a single article inside the collection, but it isn't showing up.
Is there any way to realise this?
Edit: https://talk.jekyllrb.com/t/list-collection-with-name-from-front-matter/3619/6
Or is there a way to use paragraphs in the Front Matter, because then I could just use arrays and objects in the Front Matter of the posts to do my magic?
Edit: https://github.com/jekyll/jekyll/issues/246#issuecomment-1639375
Or am I just stretching to far from what Jekyll is supposed to be?
Edit
So I found out I was getting the collection the wrong way, https://talk.jekyllrb.com/t/list-collection-with-name-from-front-matter/3619/6.
{% for section in site.[page.collectionid] %}
{{ section.content | textilize }}
{% endfor %}
The only weird part is, everywhere I see | textilize being used but can't get it to work.
This is the final solutions:
{% for section in site.[page.collectionid] %}
{{ section.content | markdownify }}
{% endfor %}
It's markdownify instead of textilize.
Also I ended up using a Front Matter array with objects to loop through instead of a collection to keep it more organized.
I have this Liquid template which looks like this:
# /_includes/slideshow.html
{% for image in {{ include.images }} %}
...
{% endfor %}
which I'm trying to use with a YAML file (for my Jekyll site) like this:
# /index.md
{% include slideshow.html images='site.data.homepage_images' %}
The reason I see this failing is because my include variable {{ include.images }} resolves to a string within the for loop. Is there a different way to accomplish this? I'm still rather new to Liquid, YAML, Jekyll, and well, web development altogether, so any help in doing this is much appreciated!
(Note: the problem goes away if I replace {{ include.images }} with site.data.homepage_images.)
Additionally, the reason why I'm doing this (and why that crude fix isn't the solution I'm looking for) is for the ability to inject my image slideshow elsewhere around my site. It'd save a lot of code to abuse my include variable in this way.
Correct syntax in for loop is : {% for image in include.images %}
Based on this tutorial, I built a multi-lingual Jekyll site. In every page, the following code links to the versions of the same page in different languages:
{% assign pages=site.pages | where:"id", page.id | sort: 'path' %}
{% for p in pages %}
<img src="{{ site.baseurl }}/images/{{ p.lang }}.png" alt="This page in {{ p.lang }}"/>
{% endfor %}
This works well, but I want to do the same in pages that belong to other collections besides "site.pages". For example, I have a collection called "site.TOPICS". I would like to replace the first line with something like:
{% assign pages=site.pages + site.TOPICS | where:"id", page.id | sort: 'path' %}
or:
{% assign pages=site.EVERYTHING | where:"id", page.id | sort: 'path' %}
But this does not work.
Is there a way to concatenate two collections? Or alternatively, to access all objects in the site regardless of type?
Right now (Jekyll 3.1.x I don't think you can do it.
I found: https://github.com/Shopify/liquid/issues/427
Which seems to show it is coming - this is an issue that looks like it is solved in Liquid, but a higher version of liquid that Jekyll uses.
I also found this: https://github.com/mpc-hc/mpc-hc.org/commit/624a4bf63710ce00d98c80f8b3655a71c0468747#diff-23a9de36055c1aa13a62d73b9c318ebd
where parker says this is a potential 4.x feature.
It looks like if you have more than one collection you could use site.documents to do this, I did a little test and while it sounds like site.documents is all docs in the site, it looks like it is really all docs in collections.
There is a push filter that maybe you could use to push one array into another, but I don't think it is really made to add 2 arrays together.
In the meantime I think just duplicating your code and having one for pages and one for collections would work. If I understand correctly your id would only be found if it matched so one would find something the other wouldn't.
I have some code that I've been repeating a lot in one of my jinja2 templates. When I've been turning a string into a link I want to check to see if it has a trailing / at the end and if it does then truncate it. This is what I'd like it to look like.
{% macro remove_trailing_slash(path) %}
{% if path[-1:] == '/' %}
{{ path[:-1] }}
{% else %}
{{ path }}
{% endif %}
{% endmacro %}
The problem I'm having is figuring out how to pass the modified path back to the original caller. I can't seem to find a return statement in the jinja2 docs.
This is something that would be better done with a filter in Jinja, rather than a macro.
In my understanding macros are for reusing pieces of logic, they don't really map to functions in Python (it would be within the Jinja compiler's right to copy the contents of the macro to every place the macro is invoked). Filters, on the other hand, are designed to manipulate the template's data before it is passed on to the "output stream".
If you register a filter with the Jinja environment, then you can do something like this:
{{ one_url | remove_trailing_slash }}
{{ another_url | remove_trailing_slash }}
If you are doing this all over your templates, you may be better off sanitizing these values before even passing them off to your templates.
You can also create a macro to wrap this pattern:
{% macro link(url) %}
{{ url | remove_trailing_slash }}
{% endmacro %}