用于元标题和标题标签的Jinja模板?

时间:2016-09-20 20:18:51

标签: javascript html jinja2

我是Jinja模板的新手。在下面的HTML中,两个元标记和标题标记的内容带有字母" A"。我需要使用" A"创建另外三个html文件。在这些标签中替换为" B"在一个文件中," C"在另一个," D"在最后一个。我怎么在Jinja这样做?有没有办法创建一个包含这些元标记和标题标记的代码块,以插入下面的主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>

2 个答案:

答案 0 :(得分:1)

您还可以将元部分定义为macro

{% 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 %}

答案 1 :(得分:0)

您可以{% 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 %}

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