I have several similar (sub)sections with a variable in the [extra] part of their _index.md.
I'd like to use this variable in the template of the pages in these sections.
The only way I've seen variables from a section used in a page template is by hard-coding the path to the section _index.md in the page template. Doing that would mean duplicating the page template for each section with the only difference between them being this path.
{% set section = get_section(path="a-section/a-subsection/_index.md") %}
{% set section = get_section(path="a-section/another-subsection/_index.md") %}
md5-8458c7f686a1006eada1d0bd3559eeb1
.
โโโ content
โโโ a-section
โโโ _index.md
โโโ a-subsection
โ โโโ _index.md
โ โโโ a-page.md
โ โโโ another-page.md
โโโ another-subsection
โย ย โโโ _index.md
โย ย โโโ yet-another-page.md
โโโ โฆ
Not possible currently no
Wellโฆ it is, but it's ugly :sweat_smile:
{% extends "index.html" %}
{% block title %}A little something from {{page.extra.author }} - {% endblock title %}
{% block description %}{{ page.description }}{% endblock description %}
{% block content %}
{% set parent_section = get_section(path="a-section/_index.md") %}
{% for subsection in parent_section.subsections %}
{% for page in subsection.pages %}
{% if current_path == page.path %}
{% set section = subsection %}
<nav><a href="{{section.permalink}}">Back to the subsection</a></nav>
<section>
<div>Author: {{ page.extra.author }}</div>
<div>Score: {{ page.extra.points }} / {{ section.extra.points }}</div>
{{page.content | safe}}
</section>
{% endif %}
{% endfor %}
{% endfor %}
{% endblock content %}
Yeah I do something a bit similar for the gitbook theme but it is not great
Possibly related: the Hyde theme has a <title> element with config.title. Is it possible to refer to the page title (as set in the front matter) instead?
page.title doesn't seem to work:
Reason: Variable `page.title` not found in context while rendering 'index.html'
Reason: Variable
page.titlenot found in context while rendering 'index.html'
The index page is actually considered a section, so you need to use section.title in index.html. Hope that helps :)
That doesn't seem to work:
Reason: Failed to render 'page.html' (error happened in a parent template)
Reason: Variable `section.title` not found in context while rendering 'page.html'
EDIT: Got it, page.title works, but I need to override that block in the page template.
Reason: Failed to render 'page.html'
Well page.html is the template for pages, so in there titles are available as page.title. Previously the error message you posted was mentioning index.html, which is the template specific to the index page, and in there the title is section.title.
Am i more clear like this? :)
Am i more clear like this? :)
Yes :-).
I'm new to gutenberg and I have some issues understanding how template inheritance works. I somehow wanted page.title to work in index.html because page.html inherits from that, and I'm rendering a page. That's more similar to https://github.com/Keats/gutenberg/issues/361#issuecomment-411506348, I guess, than to the issue at hand.
how template inheritance works
There's something about it in the Tera docs (Tera is the templating engine used by gutenberg).
Basically, you can define a default value in your index.html template:
{% block title %}{{ config.title }}{% endblock title %}
And then in templates that extend the index, you can just override the value of that block with another block construct. For instance, you could have in your page.html:
{% block title %}{{ page.title }} | {{ config.title }}{% endblock title %}
The same goes with your section.html template, and other templates extending index.html :)
Any feedback on https://github.com/Keats/gutenberg/issues/480 ? I believe it should fix that
Reference to parent section has been added to #484 feedback welcome
This is present in the next branch
Most helpful comment
There's something about it in the Tera docs (Tera is the templating engine used by gutenberg).
Basically, you can define a default value in your index.html template:
And then in templates that extend the index, you can just override the value of that block with another
blockconstruct. For instance, you could have in your page.html:The same goes with your section.html template, and other templates extending index.html :)