How to access metadata when using "where" filter - jekyll

I've got a structured set of pages in a collection, and the structure looks something like this:
chapter1
section1
section2
section3
chapter2
section1
section2
section3
chapter3
section1
section2
section3
Each is a separate page that renders its own permalink.
Say I'd like to make a link to chapter3/section1, how would I do this? I'd like to use the Liquid where filter, but this seems to give me the page contents, not the metadata.
{% assign section_post = site.chapters | where:"url","chapter3/section1" %}
{{ section_post }}
This gets me the proper page, but not the right content. If I were to write this in my layout, I get nothing:
{{ section_post.title }}
What am I doing wrong? How can I get the metadata using a where filter? I've got a bunch of pages, so looping through them is super inefficient...

The problem is that a where expression returns all the objects in an array given a certain condition.
[#<Jekyll::Document _chapters/chapter3/section1 collection=chapters>]
In this case you are expecting that this list of objects just return a single item, so we can select that item with the first liquid tag (returns the first element of an array).
{% assign ch3s1 = site.chapters |
where:"id","/chapters/chapter3/section1" | first%}
title: {{ch3s1.title}}
<br>
url: {{ch3s1.url}}
would output the desired section:
title: Chapter 3 section 1
url: /chapters/chapter3/section1

Related

Semantic Mediawiki: trying to query subobjects

I've page with a long list of subojects. The properties are correctly visibile. I've successfully queried that list from the same page and not I'm trying from another page:
I've magaed to point to the page, to filter for the tag, but it doesn't work as expected: it returns many tags (while it should return only one), it returns no title and 1 url.
{{#ask: [[-Has subobject::Tutti gli articoli di Marco Crepaldi]] [[Tags::femminicidi]]
|?tags
|?title
|?url
| mainlabel=-
}}

all_products[] Not working with JSON variable

I am trying to retrieve a metafield with all_products[] but using a locale json file content as the handle.
I have created this value in the en.default.json. If I just use {{'products.product.product_handle' | t}} I receive the proper handle so I know that I'm getting the right information but for whatever reason I can't pass that value into the all_products[] function.
I've tried the following code:
{{all_products[ 'products.product.product_handle' | t ].metafields.my_fields.cardtext}}
and
{% assign text = 'products.product.product_handle' | t %}
{{all_products[text]..metafields.my_fields.cardtext}}
Json Setup:
"product_handle": "{{ handle }}"
Can anyone tell me what I'm missing or if there is another way to display this content?
The first syntax is invalid.
The second syntax is almost valid, you have double dots before metafields => ..metafields
So when you fix the above as long as you are targeting the correct translatable string and metafield it should work:
{% assign text = 'products.product.product_handle' | t %}
{{all_products[text].metafields.my_fields.cardtext}}
You can check if you are getting the product first, for example:
{{all_products[text].id}}
If you are getting the product but not the metafield, then the issue might be in the metafield target.

Jekyll "where" filter with site.data doesn't seem to work as expected

I am trying to use what seems like a straight-forward construct to get a single "record" out of one of my data files by using the where filter, without much luck.
I am able to use most of the liquid features/constructs successfully but this one has me stumped. It does not produce an error in the build console, and I know for certain that the data in the file is good because I can accomplish the same thing via a less optimal route of using a for loop and only using the one record I need. Using this assign with the where filter seems to build fine but the place where I expect to see the output is blank.
Here is the important code:
products.yml:
-
id: FL01
name: Standard Kit
title: Scoring Area
price: $80
svg: ScoringAreasSvg
-
id: FL05
name: Full-field Kit
title: Full Field
price: $220
svg: FullFieldSvg
template:
<div>
{% assign theproduct = site.data.products | where:"productId", "FL01" %}
<p>{{ theproduct.name }}</p>
</div>
I would expect to see Standard Kit in the <p> but it is just empty.
I think you should write:
{% assign theproduct = site.data.products | where:"id", "FL01" | first %}

Conditional categories

I'm using a template {{MyTemplate|foo}} which consists of the following code:
{{#switch: {{{1}}}
| foo = [[Category:Foo]]
| bar = [[Category:Bar]]
| #default = [[Category:Unknown]]
}}
When I'm using it on a page, the categories show up nicely as wished at the bottom of that page. But unfortunately inside of that category-page (Category:Foo for example) the page doesn't show up.
If I add [[Category:Foo]] manually to that page, the page shows up in the Foo-category-page.
Any suggestions why this doesn't work via the template?
Does the template page that contain the previous code appear under Unknown category?. If yes, then any page contain that template must appear in it's category. You may just have to clear cache, and wait few seconds if you have a large wiki.
Make sure that foo & bar categories are not hidden in your wiki. See this: https://www.mediawiki.org/wiki/Help:Categories#Hidden_categories
EDIT
Also, try one of these solutions, which can do the same goal:
{{#switch: {{{1}}}
| foo = [[Category:Foo]]
| bar = [[Category:Bar]]
| #default = [[Category:Unknown]]
}}
OR
{{#ifeq: {{{1}}} | foo | [[Category:Foo]] | {{#ifeq: {{{1}}} | bar | [[Category:Bar]] | [[Category:Unknown]]}} }}
Do you run the jobqueue on your wiki? Linkstables are updated asynchronously and not at page save time.
https://www.mediawiki.org/wiki/Manual:Job_queue
https://www.mediawiki.org/wiki/Manual:RunJobs.php
Cant understand the description to your problem, but anyway, it seems like you could achieve the same goal by:
{{#vardefine: my_category |wow}}
[[Category: {{#ifeq: {{#var:my_category}}| default| unknown| {{#var:my_category}} }} ]]
Which adds this category to the page,
In my opinion, you should use switch when you are adding category in any case but also decide not to add category in another case.
If you are adding category in all cases, you can write as i wrote above, or export the final category to another variable and use it inside the category.
As for your problem, you can use "?action=purge" for this page, if that wont work, write here some more info.

Semantic mediawiki #ask query: Displaying nested properties on the same query

I would like to display in the same query properties of a page which is related to the pages im querying for.
Let's say I would like to query all the pages in the City category, which are located in Germany, and I want to display the title of the page, but also I want to display the surface data of Germany, for example.
Something like this: {{#ask: [[Category:City]] [[location::Germany]] |?mainlabel |?Location.surface }}
I know this wont work, but you can see what I want to achieve.
I'm not sure if there's a way to nest queries directly inside other queries. The normal method of doing it is using a template. So you might define a template (or subpage of the template if this going into a template) called {{tablerow}} that consists of:
<includeonly>
|- valign="top"
| [[{{{1|}}}]]
| {{#show: {{{1|}}} | ?surface }}</includeonly>
The <includeonly> tags are important for reasons I don't really understand, it produces errors sometimes if you leave them out. Then you just run an #ask query with format = template. (You can build the header into the query, but I find it simpler to just put it outside.)
{| class="wikitable smwtable sortable"
|- valign="bottom"
! [[City]]
! [[Surface]]
{{#ask: [[Category:City]] [[location::Germany]]
| format = template
| template = tablerow
| link = none
}}
|}
That will punch each result returned by the query through the template as {{{1}}} and generate a row based on it. If you have other data to return from the main query, additional properties that you ask for will come out as consecutive unnamed parameters (so if you include | ?population, that will go into the template as {{{2}}} and will need to be added to the row structure or it will be dropped).