How to get contenttype options in Bolt CMS - bolt-cms

I have contenttype for products:
products_de:
name: Products (de)
singular_name: Product (de)
slug: produkte
singular_slug: produkt
tablename: products_de
fields:
...
I've set slug for german site version links. But how I can get contenttype slug from my template? I need it automaticly, because I want to add contenttype products_en with slug products for english site version.
For example, in listing template I can do
{% setcontent products = 'products_de' %}
But its get me contenttype records but not contenttype options. There is 'contenttype' key in array record, but there is may not be any products, so I'll can't get it.
Is there any global methot to get contenttype options, something like config:
{{ config.get('general/sitename') }}
UPD: I am interested in the same for taxonomies - how to get taxonomy options such as name, slug?

I found a solution. Contenttype data can be obtained with:
{{ config.get('contenttypes/pages') }}
Taxonomy data can be obtained with:
{{ config.get('taxonomy/categories') }}
So you can do just:
{% set products_data = config.get('contenttypes/products_de') %}
{{ products_data.name }}
{{ products_data.slug }}

Related

jekyll find filename portion of page url

I need to extract the filename portion of the page url, that is from a post saved in 2011-12-31-new-years-eve-is-awesome.md I would like to just the part new-years-eve-is-awesome .
Unfortunately post_url contains also the directory tree 2011/12/31/
This page https://jekyllrb.com/docs/permalinks/ seems to suggest that defining
---
shorttitle: :title
---
in the front matter should work but that produces an empty string from {{ post.shorttitle }}
Here is my solution so far. Perhaps there is a variable left in the code but it's not documented, therefore I just filter the post URL:
{% assign spliturl = post.url | split: '/' %}
{% assign postname = spliturl[4] | replace: '.html', '' %}
{{ postname }}

Jinja2 whitespace controls doesn't work as expected with IF block

First of all: I read thoroughly documentation about whitespace control in Jinja documentation!
Have simple Jinja template:
New client has been created.
{% if not client.address %}!! Client does not have address{% endif %}
{% if not client.first_name or not client.last_name %}!! Client does not have first/last names{% endif %}
`C{{ client.id }} {{ client.full_name or "##MISSING##" }}
{{ client.status }}
When client hasn't address and first name it is rendering correct:
New client has been created.
!! Client does not have address
!! Client does not have first/last names
`C123 Some Name
pending
But when client has address or name:
New client has been created.
`C123 Some Name
pending
if expectedly produces empty lines.
Reason why I cannot user {%- if -%}: in this case second example rendering correctly BUT first example joins together both lines with "ifs".
Question: how to keep lines separate in the case then if conditions are true AND remove newlines when it falsy?
UPD: This is how I instantiate my Jinja environment:
from jinja2 import Environment, PackageLoader, select_autoescape
loader = PackageLoader("tally2bot", "templates")
env = Environment(
loader=loader,
autoescape=select_autoescape(["html", "xml", "jinja2"])
)
try this snippet below:
New client has been created.
{%- if not client.address %}
!! Client does not have address
{%- endif %}
{%- if not client.first_name or not client.last_name %}
!! Client does not have first/last names
{%- endif %}
`C{{ client.id }} {{ client.full_name or "##MISSING##" }}
{{ client.status }}
__ init__.py
from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(
loader=PackageLoader("app", "templates"),
autoescape=select_autoescape(["html", "xml", "jinja2"])
)
template = env.get_template('index.html')
print(template.render({'client': None}))
print('--------------------------------------------------------------------------------')
client = {
'id': 123,
'first_name': 'John',
'last_name': 'DOE',
'full_name': 'John DOE',
'address': 'CA',
'status': 'pending'
}
print(template.render({'client': client}))
result:
(venv) PS D:\dev\python\app> py .\__init__.py
New client has been created.
!! Client does not have address
!! Client does not have first/last names
`C ##MISSING##
pending
--------------------------------------------------------------------------------
New client has been created.
`C123 John DOE
pending

Looping through a single _data file in Jekyll and grouping the results

Setup: Currently I have a script that reads a google spreadsheet and writes the results to a *.yaml file in my _data folder on my GitHub pages site.
A typical results looks like this:
- ip: 192.168.0.1
mac: 'a4:13:4e:5e:2c:c0'
hostname: XWR-3100V1 Wireless Router
vendor: Luxul
dns_name: XWR-3100.lan
mdns_name: test
smb_name: test
smb_domain: test
comments: test
type: Router
- ip: 192.168.0.4
mac: 'b0:6e:bf:9c:11:00'
hostname: Toshiba eStudio 4505ac
vendor: ''
dns_name: ''
mdns_name: ''
smb_name: ''
smb_domain: ''
comments: ''
type: Printer
From there, I've created a page that displays a table that loops through the information and displays all of the devices.
I can't figure out the syntax for the following:
Look through the data and identify all of the different Types (Router, Media Player, Printer) etc.
Create the table with Type as the group header, and then list the records that match that type below the header, then move on to the next type, and so on.
Is this possible with a single yaml file, or do I need to some-how generate another file that contains ONLY the list of types found in the first file?
Thanks!
{% assign devices = site.data.network %}
{% for ip in devices %} //not sure what this really means
{{ip.ip}} | {{ip.mac}} ...etc.
{% endfor %}
I excluded the table tags for brevity.
Use group_by filter :
{% assign net = site.data.network | group_by:"type" %}
{% for type in net %}
<h1>{{ type.name }}</h1>
<ul>
{% for item in type.items %}
<li>{{ item.ip }} ...</li>
{% endfor %}
</ul>
{% endfor %}

Jekyll site.categories incorrect values

When I access site.categories.first I get what looks to be all the content of all my blog posts wrapped into a single string.
When I access site.categories[1] I get an empty string. The length of site.categories appears roughly equal to the number of categories I have.
I checked for any manual editing of site.categories, but I don't see anything that would be doing this.
You can use inspect filter to understand how categories works.
{{ site.categories | inspect }} returns a hash like:
{
"jekyll"=>[#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>],
"update"=>[#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>]
}
And {{ site.categories.first | inspect }} returns an array like :
["jekyll", [#<Jekyll::Document _posts/2017-10-31-welcome-to-jekyll.markdown collection=posts>]]
Where {{ site.categories.first[0] }} is the category name, and {{ site.categories.first[1] }} is an array containing first category's document.
You can call a category from his name {{ site.categories.jekyll | inspect }} but not by is index {{ site.categories[0] | inspect }} => []
You cannot modify site.categories because it is freezed by jekyll.

Show a collection of values in jekyll

Is it possible to have a collection of values in jekyll and then format them as table?
I tried something like this in my .md file:
---
layout: tutorial
title: Jekyll
reqs:
- name: names here
desc: description here
value: value
- name: names here
desc: description here
value: value
---
In my tutorial layout I have this:
---
layout: home
---
{% for item in page.reqs %}
{{ item.name }}
{{ item.desc }}
{{ item.value }}
{% endfor %}
The html code for the table was removed. The problem is my for loop prints nothing. The page is empty except for what was inherited from the other layout.
Looks like a YAML problem. You're mixing up the array and dictionary syntaxes. Try something like this instead:
---
layout: tutorial
title: Jekyll
reqs:
- item1:
name: names here
desc: description here
value: value
- item2:
name: names here
desc: description here
value: value
---
Now your for loop is looping through an array of dictionaries (item1, item2...) whose values you can use in your output.