元标签和标题标签的Jinja模板

Jinja templates for meta and title tags?

本文关键字:标签 Jinja 模板 标题      更新时间:2023-09-26

我是Jinja模板新手。在我下面的HTML中,两个元标签和标题标签的内容是字母"A"。我需要创建三个以上的html文件与"A"在这些标签替换为"B"在一个文件,"C"在另一个,和"D"在最后一个。在金贾怎么做?是否有一种方法来创建一个包含这些元标签和标题标签的代码块,以插入下面的主要HTML ?我如何使用Javascript在每个页面的顶部指定需要使用的字母?

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="shortcut icon" type="image/x-icon" href="/images/favicon.ico">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="robots" content="noindex, nofollow">
        <meta name="description" content="Let us help you with your "A" needs. Get "A" today.">
        <meta name="keywords" content="A">
        <title>"A" Quote</title>
        <script src="raven.min.js"></script>
        <script>Raven.config('https://getsentry.com/90500').install();</script>
        <link rel="stylesheet" type="text/css" href="/css/vendor.css">
        <link rel="stylesheet" type="text/css" href="/css/main.css">
    </head>
    <body itemscope itemtype="">
        <div id="navigation"></div>
        <div id="my_page"></div>
        <div id="footer"></div>
        <!-- Our scripts -->
        <script type="text/javascript" src="/js/vendor.js"></script>
        <script type="text/javascript" src="/js/sem.js"></script>
    </body>
</html>

您也可以将元节定义为宏。

{% macro meta_header(value='A') -%}
<meta name="description" content="Let us help you with your {{value}} needs. Get {{value}} today.">
<meta name="keywords" content={{value}}>
<title>{{value}} Quote</title>
{%- endmacro %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="shortcut icon" type="image/x-icon" href="/images/favicon.ico">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        {% meta_header('B') %}
        <script src="raven.min.js"></script>
        <script>Raven.config('https://getsentry.com/90500').install();</script>
        <link rel="stylesheet" type="text/css" href="/css/vendor.css">
        <link rel="stylesheet" type="text/css" href="/css/main.css">
</head>
    <body itemscope itemtype="">
        <div id="navigation"></div>
        <div id="my_page"></div>
        <div id="footer"></div>
    </body>
</html>

在任何其他文件中,您可以导入宏(如果需要):

{% from 'main.html' import meta_header %}

您可以使用您需要的标记来{% include %}文件。下面是一个例子:

Main.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="shortcut icon" type="image/x-icon" href="/images/favicon.ico">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        {% include "meta_A.html" %}
        <script src="raven.min.js"></script>
        <script>Raven.config('https://getsentry.com/90500').install();</script>
        <link rel="stylesheet" type="text/css" href="/css/vendor.css">
        <link rel="stylesheet" type="text/css" href="/css/main.css">
</head>
    <body itemscope itemtype="">
        <div id="navigation"></div>
        <div id="my_page"></div>
        <div id="footer"></div>
        <!-- Our scripts -->
        <script type="text/javascript" src="/js/vendor.js"></script>
        <script type="text/javascript" src="/js/sem.js"></script>
    </body>
</html>

meta_A.html

<meta name="robots" content="noindex, nofollow">
<meta name="description" content="Let us help you with your "A" needs. Get "A" today.">
<meta name="keywords" content="A">
<title>"A" Quote</title>

然后你可以有文件meta_B.html等等。如果你有一个变量来指示你需要哪个文件,a, B,等等,那么你可以用jinja在main.html中添加一点逻辑,像这样:

{% if my_variable == 'A' %}
    {% include "meta_A.html" %}
{% elif my_varaible == 'B' %}
    {% include "meta_B.html" %}        
{% elif my_varaible == 'C' %}
    {% include "meta_C.html" %}            
{% elif my_varaible == 'D' %}
    {% include "meta_D.html" %}        
{% endif %}

您只需在main.html的第一个示例中替换include语句的位置。