简体   繁体   English

将 Markdown 渲染到 Jinja2 模板中的多个块

[英]Rendering Markdown to multiple blocks in Jinja2 Template

So lets say I have a base template and I want to have two blocks - one named primary and one named secondary.所以假设我有一个基本模板,我想要两个块 - 一个命名为主要的,一个命名为辅助的。 When I go to write my markdown file, is it possible to render sections of my markdown to both blocks?当我 go 写入我的 markdown 文件时,是否可以将我的 markdown 的部分渲染到两个块? I was reading through markdown documentation and Jinja documentation and I didn't really see a clear answer to this as far as I could tell.我正在阅读 markdown 文档和 Jinja 文档,据我所知,我并没有真正看到明确的答案。

You can create a base template that contains as many blocks as you would like, and then define new pages that extend the base template and define the content that should go in those blocks.您可以创建一个包含任意数量块的基本模板,然后定义扩展基本模板的新页面并定义这些块中应包含 go 的内容。 A simple example:一个简单的例子:

base.html底座.html

<div>
{% block my_primary_block %}
{% endblock %}
</div>

<div>
{% block my_secondary_block %}
{% endblock %}
</div>

Now, supposing you want to populate those blocks from another HTML file, you could define the content of each block:现在,假设您想从另一个 HTML 文件填充这些块,您可以定义每个块的内容:

article.html文章.html

{% extends "base.html" %}

{% block my_primary_block %}
Hello world!
{% endblock %}

{% block my_secondary_block %}
Goodbye world!
{% endblock %}

but since you asked about markdown, you can use variables from articles using {{ article.variable_name }} from the HTML, and then define variable_name in an article header.但是由于您询问了 markdown,您可以使用 HTML 中的{{ article.variable_name }}的文章中的变量,然后在文章 header 中定义variable_name For example,例如,

article.html文章.html

{% extends "base.html" %}

{% block my_primary_block %}
{{ article.primary }}
{% endblock %}

{% block my_secondary_block %}
{{ article.secondary }}
{% endblock %}

then in your article, you just add those fields to the metadata for the article:然后在您的文章中,您只需将这些字段添加到文章的元数据中:

content/my_blog_post.md内容/my_blog_post.md

Title: My cool blog post
Date: 2000-01-01 00:00
Primary: Hello world!
Secondary: Goodbye world!

This is the content of my cool blog post.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM