Render Jekyll collection metadata to each output document using layout template - collections

Given a custom Jekyll collection in a folder _stuff, where each document has some metadata, how do I render that metadata using a layout template to an output document? I suspect I need a plugin to do any more than iterating over a collection. E.g. in _config.yml:
collections:
stuff:
output: true
permalink: /stuff/:path
In collection item, e.g. _stuff/thing1.md:
---
title: Thing 1
some_data: 123
layout: stuff-detail
---
Using layout, stuff-detail.liquid:
---
....
---
<div>Stuff Item Data: {{ page.some_data }} (doesn't work)</div>

In your stuff-detail.liquid file you'll need double brackets around your liquid call: {{ page.some_data }}

Try to use this code on your layout stuff-detail.liquid:
<div>Stuff Item Data: {{ page.output }}</div>
See how it works as explained here.

Related

How to display the Content type custom fields in twig template?

I am very new to Drupal.
Can anyone tell me how to display the custom fields content type into twig template?
Thank you
I used below code with content and nodes.
{{ content.field_a }}
You'll have to follow these steps :
Step 1) Custom template for a content type
Suppose, you have to create a new template for content type i.e. Article ( machine_name : article ). Just make a copy of node.html.twig and rename with node--article.html.twig
Step 2) Call custom field
In this template, you can display content of your field like {{ content.field_test_field }}
Step 3) Clear Cache
If you're looking to create a full page template for an Article for example, you can create a file called : page--node--article.html.twig
Any custom content type would follow the pattern, for example if the content type is called Machine Product, the template would be: page--node--machine-product.html.twig (1 dash between spaces of the content type name.
For Drupal 8, this is my typical mapping for this filetype :
Blocks:
{{ page.REGIONNAME }}
Title:
{{ node.title.value }}
Taxonomy Terms/Dropdown Selects in Create Content:
{{ node.field_FIELDNAME.0.entity.label }}
General Text Fields:
{{ node.field_FIELDNAME.value }}
Images/Files:
{{ file_url(node.field_FIELDNAME.entity.fileuri')) }}

SeoBundle - title and description are not set

I have configured SeoBundle as stated in docs:
sonata_seo:
page:
title: Default title
metas:
name:
description: Defualt description
keywords: key1, key2
cmf_seo:
title: seo.title
description: seo.description
content_listener:
enabled: false
And placed in base.html.twig:
<head>
{# meta #}
<meta name=viewport content="width=device-width, initial-scale=1">
<meta name="format-detection" content="telephone=no"/>
{{ sonata_seo_title() }}
{{ sonata_seo_metadatas() }}
...
</head>
Now when I refresh page I can see that sonata_seo block title and description are set instead of cmf_seo. In docs there is a warning:
The title and description template is only used when the title is not
set on the content object or when the content object is not available,
otherwise it'll use the default set by the SonataSeoBundle. You should
make sure that the defaults also follow the template.
If only docs would clarify what is "content object" would be great... Anyway I tried removing title and description from sonata_seo block:
sonata_seo:
page: ~
cmf_seo:
title: seo.title
description: seo.description
content_listener:
enabled: false
Now my title becomes "Sonata Project" which is default SonataSeoBundle title.
How do I display cmf_seo title? {{ cmf_seo_title() }} is not working.
what is the value of seo.title and seo.description is it something you expect to be translated? Can you serve a longer stacktrace for the exception please?
In general CmfSeoBundle expects some placeholders (%%content_description%%, %%content_title%%, doubled to make it translatable at the end) to enrich the title/description with dynamic data. That dynamic data can live as SeoContent on the document (content object) you are persisting or as so called extractors which fetch the data from the document (content object). In the first case you have to implement SeoAwareInterface in second case you can find several extractors in the [doucmentation[(https://symfony.com/doc/current/cmf/bundles/seo/extractors.html).
If you wanna simply set static titles, you should stay with the SonataSeoBundle. We do simply enrich a template defined in cmf_seo.title|description with dynamic data from the current content. We do need sonata_seo.title for pages with no content documents (i.e. static pages).

Assemble.io - Looping around a YAML List : {{each}} vs {{eachProperty}}

If I have a YAML list, e.g:
Home : /
Terms and Conditions : /terms.html
And I want to render some content from that list, but make use of both keys & values, I'm having to use {{eachProperty}} to get those values, i.e :
{{#eachProperty this.value}}
{{key}}
{{/eachProperty}}
Anyone got any better ideas?
I think you just need to add another layer to your YAML data structure, so you have a list of pages, where each page is an object that has both a title and url property you can reference in the template. In the example below, I put the YAML at the top of the Handlebars template, but it should work the same if loaded from an external file.
---
links:
Home:
url: '/'
title: 'Home'
Terms:
url: '/terms.html'
title: 'Terms and Conditions'
---
<!DOCTYPE html>
<html>
<body>
<h1>Test List of Links</h1>
<ul>
{{#each links}}
<li>{{title}}</li>
{{/each}}
</ul>
</body>
</html>

Liquid and Jekyll - Add CSS class on occurence of character in an array

on my site i have lists using two different kinds of styles for list items. Their occurance is uneven.
For now i edit these lists manually in html. I would like to put them inside an array in the YAML front matter and let jekyll generate the appropriate lists.
Example:
My idea is to put all list items in an array in the YAML front matter and tag those, which should be italic, with a string like 'ITALIC_':
list: [ITALIC_Main, 300g tomatoes, 1 mozzarella ball, ITALIC_Dressing, olive oil, vinegar, ...]
Is it possible to check not only the array for a certain string but
the array items too?
How can i filter the tagged array items and apply a certain css class via Jekyll?
Thanks for your help!
Vin
I think you have a modeling problem ;-) You're mixing datas and style : No Goood !
I propose a more dissociated solution, with organized datas on one place and presentation in an other.
It can be something like this :
---
title: recipe
layout: default
recipe:
ingredients:
main:
- ingredient1
- ingredient2
dressing:
- ingredient3
- ingredient4
optional:
- ingredient5
operations:
.... To be continued ...
---
<h2>Ingredients</h2>
{% for part in page.recipe.ingredients %}
<h3>{{ part[0] }}</h3>
<ul>
{% for ingredient in part[1] %}
<li>{{ ingredient }}</li>
{% endfor %}
</ul>
{% endfor %}
<h2>Operations</h2>
{% for part in page.recipe.operations %}
To be continued ...
(Not tested) You can write it as: ITALIC_Main_ and in the template loop just remove the ITALIC with
{{ site.list.item | remove_first: "ITALIC" }} - this will leave you with _Main_ which is converted to italic style in markdown.

Limit amount of words displayed from MySQL on a Twig page

I wish to limit the amount of text displayed from a mysql statement. So for example, in my database I have a page where there are 1000 words contained in the content field, I want to be able to just display 200 of those words.
How can this be done using TWIG?
Cheers!
Sounds like your are looking for the "truncate" filter.
In your app/config/config.yml add::
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
Then you can do in your templates:
{{ var.foo | truncate(200) }}
{{ "Hello good Sir!" | truncate(4) }}

Resources