I'm kind of stuck making a multiple comparison using nunjucks!
It works when I use:
{% if (tab.type != 'default') %}
but not when I use:
{% if (tab.type != 'placement') or (tab.type != "version1") or (tab.type != 'default') %}
In the latter, it keeps getting there as if everything was equal instead of different!
HELP!
Thanks
Strange! I've had the exact same problem just now. To be precise, I was checking to see if page title is not certain value and then output some code. There were two pages which are impacted by this so my first logical condition was as same as yours.
{% if title != 'Messenger' or title != 'Video' %}
OUTPUT
{% endif %}
It didn't work.
What worked for my case in the end (after countless combinations) is this:
{% if title != 'Messenger' and title !='Video' %}
OUTPUT
{% endif %}
In my case it doesn't matter too much because I can have only one page title anyway, but in your or someone else's case it may be different. Someone maybe wants to trigger the output if either one of the conditions has been met.
As far as my logics goes: and means that both conditions must be fulfilled, which clearly isn't the case here.
If you still read this, please let me know if this has worked for you.
Related
Using Eleventy with Nunjucks, and need to check if a front matter field is empty and if a string is empty. It would be the same thing as isEmpty() in JS. Does Nunjucks support this functionality? It seems like a pretty simple and common feature, but I've looked all over the docs and StackOverflow but can't find any mention or example.
Thanks :)
I tested like so:
---
name: ray
age:
---
{% if age %}
age has a value
{% else %}
age has NO value
{% endif %}
And it worked as expected, I got: "age has NO value".
I am exploring the possibilities of OctoberCMS in a test project about movies. Each movie has several properties, among others one or more genres (both movies and genres are models btw). I am now trying to code something for the front-end filters on a movie list page, but I am having problems trying to get to the field genre_title -see screenshot at https://i.stack.imgur.com/yrIHm.png. When I do {{ dump(movies.items[0].relations) }} I see the collection genres but from then on I am stuck. I have tried many different things:
{{dump(movies.items[0].relations[0].items)}}
{{dump(movies.items[0].relations['genres'].items)}}
{{dump(movies.items[0].relations.items)}}
etc.
Most of my attempts gave me NULL as a result. How can I get to the genre_title? I'd really appreciate it if someone could point me in the right direction! Many thanks!
Its collection so you can simply need to loop
{% for movie in movies %}
{% for genre in movie.genres %}
{{ genre.genre_title }} - {{ genre.slug }} - {{dump(genre)}}...
{% endfor %}
{% endfor %}
Now you can have all the data available.
if any doubt please comment.
So the current Shopify implementation of sections leaves a lot to be desired. The majority of the functionality is relegated to the homepage.
I'm trying to skirt around that to a certain degree but basically chucking all the section functionality (that would normally be split into multiple sections) into one section file, and then duplicating it for each product in the store, reusing the handle of each product as the section name.
E.g.: example-product-handle --> sections/example-product-handle.liquid
My idea was then to create, in the main product.liquid file, a simple routing system that would conditionally include a section if one exists that matches with the handle. This SO answer got my creative juices flowing.
The ideal result would look like...
{% assign current_page = product.handle %}
{% capture snippet_exists %}{% section current_page %}{% endcapture %}
{% unless snippet_exists contains "Liquid error" %}
{% section current_page %}
{% endunless %}
This works beautifully for snippets. Replace section with include in that code, and the routing system performs perfectly.
With sections however?
Liquid syntax error: Error in tag 'section' - Valid syntax: section '[type]'
Is there no way around this? Do section names have to be explicitly stated?
This isn't possible. It is purposefully not possible. Try instead using the section to dynamically include snippets.
{% for block in section.blocks %}
{% case block.type %}
{% when 'layout1' %}
{% include 'layout1' %}
{% endfor %}
I want to output a line conditionally in nunjucks, but don't want to have an empty line if the condition is false.
Example:
Before
{{ 'Something' if false }}
After
renders like this:
Before
After
I would like to remove the empty line without lowering the readability of the template. Is there a nice way to do that?
I would expect something like {{- 'Something' if false }} but that doesn't work.
Nunjucks has it's own conditional methods. Note the percent symbol instead of double moustaches.
https://mozilla.github.io/nunjucks/templating.html#if
{% if hungry %}
I am hungry
{% elif tired %}
I am tired
{% else %}
I am good!
{% endif %}
I'm using Jekyll to create a new blog. It uses Liquid underneath.
Jekyll defines certain "variables": site, content, page, post and paginator. These "variables" have several "members". For instance, post.date will return the date of a post, while post.url will return its url.
My question is: can I access a variable's member using another variable as the member name?
See the following example:
{% if my_condition %}
{% assign name = 'date' %}
{% else %}
{% assign name = 'url' %}
{% endif %}
I have a variable called name which is either 'date' or 'url'.
How can I make the liquid equivalent of post[name] in ruby?
The only way I've found is using a for loop to iterate over all the pairs (key-value) of post. Beware! It is quite horrible:
{% for property in post %}
{% if property[0] == name %}
{{ property[1] }}
{% endif %}
{% endfor %}
Argh! I hope there is a better way.
Thanks.
I don't know what I was thinking.
post[name] is a perfectly valid liquid construction. So the for-if code above can be replaced by this:
{{ post[name] }}
I thought that I tried this, but apparently I didn't. D'oh!
Liquid admits even fancier constructs; the following one is syntactically correct, and will return the expected value if post, element, categories, etc are correctly defined:
{{ post[element.id].categories[1].name }}
I am greatly surprised with Liquid. Will definitively continue investigating.
Actually, this did not work for me. I tried a bunch of different combinations and what finally worked was
<!-- object myObj.test has the string value "this is a test" -->
{% assign x = 'test' %}
{{ myObj.[x] }}
Output:
this is a test