FreeMarker宏macro的使用

定义宏macro

创建public.ftl文件,用于存放各个公共宏macro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<#--公共顶部-->
<#macro header title="默认文字" keywords="默认文字" description="默认文字">
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
<meta name="format-detection" content="telephone=no" />
<title>${title}</title>
<meta name="keywords" content="${keywords}" />
<meta name="description" content="${description}" />

<link rel="stylesheet" type='text/css' href="/static/style/common.css"/>

<#nested>

</head>
<body>
</#macro>

<#--公共底部-->
<#macro footer>

<script type="text/javascript" src="/static/js/common.js"></script>

<#nested>

</body>
</html>
</#macro>

引用宏macro

创建示例页面about.ftl,此页面中使用宏macro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<#include "/include/public.ftl">

<@header title="公司简介" keywords="公司简介2" description="公司简介3">

<link rel="stylesheet" type='text/css' href="/static/style/about.css"/>
<style type="text/css">
body{background:#fff}
.pub-title{margin-bottom:0;padding-bottom:15px}
.pub-con p{color:#555;line-height:30px;padding:0 30px}
</style>

</@header>

<div class="pub-con">
<p>
FreeMarker宏macro的使用
</p>
</div>


<@footer>

<script type="text/javascript" src="/static/js/about.js"></script>

</@footer>

结果示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
<meta name="format-detection" content="telephone=no" />
<title>公司简介</title>
<meta name="keywords" content="公司简介2" />
<meta name="description" content="公司简介3" />

<link rel="stylesheet" type='text/css' href="/static/style/common.css"/>

<link rel="stylesheet" type='text/css' href="/static/style/about.css"/>
<style type="text/css">
body{background:#fff}
.pub-title{margin-bottom:0;padding-bottom:15px}
.pub-con p{color:#555;line-height:30px;padding:0 30px}
</style>

</head>
<body>

<div class="pub-con">
<p>
FreeMarker宏macro的使用
</p>
</div>

<script type="text/javascript" src="/static/js/common.js"></script>

<script type="text/javascript" src="/static/js/about.js"></script>

</body>
</html>

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :