布局模板 - 自定义 HTML 布局
2021-11-16
布局模板基础使用方法
可以使用主题自带的模板,比如错误页面,可以直接使用404.html, 只需要在metadata中添加layout键值即可
---
layout: 404.html
---
自定义布局模板
首先写布局模板文件, 支持 Jinja2 语法, 写法可以参考默认主题插件的布局模板的写法.
模板文件可以放到两个地方, 一个是放到主题插件里面, 插件作者可以这样做;
另一个就是放到布局模板文件夹(默认文档根目录layout)下, 然后单独对特定的页面使用特定的布局模板, 在文章头部metadata处添加layout键值:
layout: 页面使用的布局模板, 默认不需要这个键值, 会使用主题插件里面的配置,需要你需要自定义这个页面的布局, 可以设置这个参数, 路径相对于site_config中的layout_root_dir路径, layout_root_dir 默认为layout, 所以要使用layout/special_layout.html 只需要填写special_layout.html
可以基于主题的模板修改,或者继承主题的模板,比如小改一下404.html页面的body部分,只需在layout目录下新建一个my_404.html, 注意文件名不能和内置的文件名冲突, 如果冲突会提示generate html fail: maximum recursion depth exceeded in comparison:
{% extends "404.html" %}
{% block body_404 %}
这里是 body
{{ body|safe }}
{% endblock%}
然后在404.md中使用这个模板
---
layout: my_404.html
---
jinja2 模板变量
这里列出了所有的支持的变量:
page head info
lang: language, e.g.en, orzh-CNmetadata: metadata from doc metadata, dict typepage_id: page id , a string, set in doc config and doc metadatapage_classes: page classes, a list, set in doc config and doc metadatakeywords: keywords, a list, set in doc metadatadescription: description, a string, set in doc metadataheader_items: tags in head(<head></head>) tag, a string listtitle: page title, can be null, string, set in doc metadatasite_name: site name, string, set in site configjs_vars: js variables set and used by plugins, a dict object
navbar
logo_url: site logo image url, string, can be null, set in doc configlogo_alt: site logo alt info, string, can be null, set in doc confighome_url: home url, e.g. "/", "/site0/", stringnavbar_title: navbar title, html string, can be null, set in doc confignavbar_main: navbar left html, html stringnavbar_options: navbar right html, html stringnavbar_plugins: navbar plugins html, html string
sidebar info
pages no sidebar, only article of docs have
sidebar_title: sidebar title, string, can be html string, set in doc sidebar configsidebar_items_html: sidebar items html, html string
body info
article of docs body info
article_title: doc's article title, string, set in doc metadatatags: article tags, a string list, set in doc metadataauthor: article author, string, can be null, set in doc metadatadate: article create date, string, can be null, set in doc metadatashow_source: show source button info, string, can be null, set in site config and doc metadatasource_url: source file's url, valid only when show_source is not nullbody: body html, html stringprevious: previous article, can be nulltitle: previous article title, stringurl: previous article url, string
next: next articletitle: next article title, stringurl: next article url, string
toc: toc html string, can be null
pages body info
body: body html, html string
page footer info
footer_top: footer top html stringfooter_bottom: footer bottom html stringfooter_js_items: js items last load int html, string list