Page meta
attribute missing in include
#7995
-
ContextNo response Bug descriptionIf I include a file in a template with
To confirm that the variable exists, I check
But if I use the
The This is what happens when I use just > mkdocs build -c
INFO - Cleaning site directory
INFO - Building documentation to directory: /mkdocs/site
Traceback (most recent call last):
File "/usr/local/bin/mkdocs", line 8, in <module>
sys.exit(cli())
^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1161, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1082, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1697, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1443, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 788, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/mkdocs/__main__.py", line 288, in build_command
build.build(cfg, dirty=not clean)
File "/usr/local/lib/python3.11/site-packages/mkdocs/commands/build.py", line 328, in build
_build_theme_template(template, env, files, config, nav)
File "/usr/local/lib/python3.11/site-packages/mkdocs/commands/build.py", line 103, in _build_theme_template
output = _build_template(template_name, template, files, config, nav)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/mkdocs/commands/build.py", line 83, in _build_template
output = template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1295, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 942, in handle_exception
raise rewrite_traceback_stack(source=source)
File "/usr/local/lib/python3.11/site-packages/material/templates/404.html", line 4, in top-level template code
{% extends "main.html" %}
File "/mkdocs/overrides/main.html", line 1, in top-level template code
{% extends "base.html" %}
File "/usr/local/lib/python3.11/site-packages/material/templates/base.html", line 140, in top-level template code
{% block header %}
^^^^^^^^^^^^^^^^^^^^^^^
File "/mkdocs/overrides/main.html", line 4, in block 'header'
{% include "header.html" %}
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mkdocs/overrides/header.html", line 2, in top-level template code
<div>page.meta.my_var (header): {{ page.meta.my_var }}</div>
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 490, in getattr
return getattr(obj, attribute)
^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'None' has no attribute 'meta' This only occurs on HTML files pulled in with Related linksReproductionSteps to reproduceThis is my configuration:
---
my_var: this is my var
---
# Introduction
Hello world
{% extends "base.html" %}
{% block header %}
{{ super() }}
{% include "header.html" %}
{% endblock %}
{% block content %}
{{ super() }}
<div>page.meta.my_var (main): {{ page.meta.my_var }}</div>
{% endblock %}
site_name: my_site
use_directory_urls: false
theme:
name: material
custom_dir: overrides This works <div>page.meta (header): {{ page.meta }}</div>
<div>page.meta.my_var (header): {{ ( page.meta | default("") ).my_var }}</div> This doesn't work: <div>page.meta (header): {{ page.meta }}</div>
<div>page.meta.my_var (header): {{ page.meta.my_var }}</div> BrowserNo response Before submitting
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Thanks for reporting. This is not an issue with Material for MkDocs, but a problem with customization of MkDocs/Jinja. I'm converting this to a discussion, as somebody else might be able to help you debug this. |
Beta Was this translation helpful? Give feedback.
-
Hello @DevOpsJeremy, File "/usr/local/lib/python3.11/site-packages/material/templates/404.html", line 4, in top-level template code So to handle such cases always do a safety guard {% if page and page.meta and page.meta.robots %}
I think that MkDocs could make things easier and add a |
Beta Was this translation helpful? Give feedback.
Hello @DevOpsJeremy,
you need to remember that your Jinja template runs not only for your Pages (which are derived from Markdown files), but also for other static templates like the
404.html
page (only this one by default), and these don't have anypage.meta
, hence theNone
error.So to handle such cases always do a safety guard
if page.meta and page.meta.variable
, which can be seen in the docs: