Let's say I have two files, button.html and toggle.html. For this example, button is irrelevant, but toggle isn't. This is the code for toggle:
{% if include.value %}
{% assign addonclass = 'addon-class' %}
{% endif %}
<div class="toggle {{addonclass}}">
Some content
</div>
Which makes sense right, I want to add a class to that div if whenver I do
{% include toggle.html value = "somevalue" %}
Here's the kicker though. On it's own, that works just fine, however, if I on a page I do the following:
{% include button.html value = "some value" %}
{% include toggle.html %}
The if statement inside toggle will be true, however, it will not output{{value}}, it'll just be empty. What the hell? Shouldn't every 'component' mind it's own props, rather than all the props defined everywhere? I understand I have to do something like:
{% assign labelclass = '' %}
But why? Why can I pass props to components, if they get passed to all components called? I'm running this with Jekyll btw, if that makes any difference.
Related
In my Jinja template, model.DataType value can be user defined or built in. My requirenement is if model.DataType start with the three letters ARR, then do a specific operation.
Example of values:
ARRstruct124
ARR_int123
ARR123123
CCHAR
UUINT
etc.
{% set evenDataType = model.eventDataType %}
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
// do the operation
{%else%}
// do the operation
{% endif %}
With this template, I am getting the error
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
jinja2.exceptions.TemplateAssertionError: no filter named 'regex_match'
There is indeed no regex_match filter in the Jinja builtin filters. You might have found some examples using it, but this is an additional filter provided by Ansible, so it won't work outside of Ansible.
This said, your requirement does not need a regex to be fulfilled, you can use the startswith() method of a Python string.
So, you template should be:
{% set evenDataType = model.eventDataType %}
{% if evenDataType.startswith('ARR') %}
`evenDataType` starts with 'ARR'
{% else %}
`evenDataType` does not starts with 'ARR'
{% endif %}
<vcpu placement='{{cpu.placement}}' cpuset='{{cpu.cpuset}}' current={{cpu.current}}>{{ cpu['maximum'] }}</vcpu>
Giving that we have 3 possible attributes and any of them can be empty.
Is there a neat way to hide the attribute definition if the value is empty? Let's say if cpu.placement is empty, the line placement='' shouldn't be there in the XML definition.
Just put the attribute definition inside an if-block:
<vcpu
{% if cpu.placement %}placement='{{cpu.placement}}'{% endif %}
{% if cpu.cpuset %}cpuset='{{cpu.cpuset}}'{% endif %}
{% if cpu.current %}current={{cpu.current}}>{{ cpu['maximum'] }}{% endif %}
</vcpu>
See the list of builtin tests in you need more complicated test for a variable.
I'm trying to see if it's possible to iterate a variable in Jekyll for a testimonial block I'm implementing for a Jekyll site. Basically, I'd like to have an icon be multiplied by the number dictated in my collection. Is this even possible with liquid markdown? Here's a snippet:
{% assign star = "<i class="icon-star"></i>" %}
{% assign star = star | times:{{ testimonials.stars }} %}
I'm thinking there's better ways to do this, but I was curious what I could get away with front matter.
To do it iterating, you can use a for loop appending the desired string to a variable:
{% assign star = '<i class="icon-star"></i>' %}
{% assign result = '' %}
{% for time in testimonials.stars %}
{% assign result = result | append: star%}
{% endfor %}
The key is to use a for block. You don't have to write multiple assign statements.
The following will render a star per the front matter key rating in any page using the code.
Modify the for bock as required.
{% assign star = '<i class="icon-star"></i>' %}
<div class="rating">
{% for count in (1..page.rating)) %}
{{ star }}
{% endfor %}
</div>
Ref: docs
I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.
I have a data file in _data/sidebars/sidebar1.yml. It contains:
- first
- second
- third
On a page I have this frontmatter:
---
title: My page
sidebar: site.data.sidebar.sidebar1
---
I want to use this code:
{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}
However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).
The only thing that works is to do this:
{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}
{% for entry in sidebar %}
* {{entry}}
{% endfor %}
However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.
Any ideas on how to make this work?
You have a typo in your front matter. It's :
sidebar: site.data.sidebars.sidebar1
not
sidebar: site.data.sidebar.sidebar1
You can even be less verbose.
In your front matter : sidebar: sidebar1
In your code :
{% assign sidebar = site.data.sidebars[page.sidebar] %}
{% for entry in sidebar %}
{{ entry | inspect }}
{% endfor %}
In Jekyll, I want to loop through items in a list and assign variables to each of them.
In a data file, my list looks like this:
entries:
- title: Sidebar
subcategories:
- title: Overview
items:
- title: Introduction
url: /introduction/
linkname: intro
- title: Release Notes
url: /release_notes/
linkname: relnote
My looping logic looks like this:
{% for entry in sidebar %}
{% for subcategory in entry.subcategories %}
{% for item in subcategory.items %}
{% assign item.linkname = "{{item.title}}" %}
{% endfor %}
{% endfor %}
{% endfor %}
Then I want to insert the variable using {{intro}} or {{relnote}} on a page. Using this method, I should be able to generate variables for all the items in my sidebar, making it easy to embed links in my content.
However, this method doesn't seem to work. Nothing appears for the variable when I insert it. What am I doing wrong?
With Jekyll/Liquid, you cannot create a variable name from a variable value.