元标签和标题标签的Jinja模板
Jinja templates for meta and title tags?
我是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
语句的位置。
相关文章:
- 在<页眉>标签
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- iframe正在添加标签,需要删除它们
- 如何自动调整标签的高度以适应内容
- Jquery标签插件粘贴问题
- Twitter引导程序Typeahead-Id&标签
- Highcharts-如何在自定义格式化程序中获得默认的y轴标签格式化程序
- Morris.js折线图x轴标签在调整大小后消失
- JavaScript goto 标签如何使用内部循环
- 显示某个用户ID的某个标签的30张Instagram图片
- 标签客户端的设置值
- 谷歌图表显示所有标签
- 如何在图片和边缘之间居中设置标签
- 在Datatables中设置本地化后,需要更改标签文本
- 第二组标签及其内容赢得'默认情况下t加载
- 在一个javascript文件中为整个网站创建标签
- 兰吉 |如何在 span 标签上使用单击事件删除突出显示的文本
- Knockout.JS标签在foreach内部不起作用
- 如何将JavaScript变量传递给Jinja标签中的函数
- 元标签和标题标签的Jinja模板