随笔 - 833  文章 - 0  评论 - 9  阅读 - 35万

freemaker宏的用法

freemaker宏

定义:定义一个标签,标签体中可以包含参数,开始标签和结束标签可以包含内容,内容中可以通过${}方式引用标签体中定义的参数

用法:页面引入标签,通过标签可以直接输出标签的内容

HelloWorld实现

复制代码
定义html.ftl:
<
#macro html title> <html> <head>   <meta http-equiv="Content-Type" content="text/html; charset=GBK" />   <title>${title}</title>   <link rel="stylesheet" rev="stylesheet" href="/oa/file/css.css" type="text/css" media="all" /> </head> <body>   <#nested/> </body> </html> </#macro>
复制代码
用法:
<#import "/WEB-INF/template/common/common.ftl" as c> 

<@c.html title="OA"> 
      你的内容 
</@c.html>
复制代码
输出结果:
<#macro html title> 
      <html> 
        <head> 
          <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> 
          <title>${title}</title> 
          <link rel="stylesheet" rev="stylesheet" href="/oa/file/css.css" type="text/css" media="all" /> 
        </head> 
        <body> 
          你的内容
        </body> 
      </html> 
</#macro> 
复制代码

相关语法

1.macro

<#macro name param1 param2 ... paramN> 
用例 
<#macro test foo bar="Bar" baaz=-1> 
Test text, and the params: ${foo}, ${bar}, ${baaz} 
</#macro> 
<@test foo="a" bar="b" baaz=5*5-2/> 
<@test foo="a" bar="b"/> 
<@test foo="a" baaz=5*5-2/> 
<@test foo="a"/> 
输出 
Test text, and the params: a, b, 23 
Test text, and the params: a, b, -1 
Test text, and the params: a, Bar, 23 
Test text, and the params: a, Bar, -1 

2.nested(<#macro name param1 param2 ... paramN;x y z> )xyz为nested 标签定义的内容,nested 相当于标签内容的占位符

<#macro repeat count> 
  <#list 1..count as x> 
       <#nested x, x/2, x==count> 
  </#list> 
</#macro> 
<@repeat count=4 ; c halfc last> 
    ${c}. ${halfc}<#if last> Last!</#if> 
</@repeat> 
输出 
1. 0.5 
2. 1 
3. 1.5 
4. 2 Last! 

3.循环

<#macro list title items> 
<p>${title?cap_first}: 
<ul> 
       <#list items as x> 
         <li>${x?cap_first} 
       </#list> 
</ul> 
</#macro> 
<@list items=["mouse", "elephant", "python"] title="Animals"/> 
输出结果 
<p>Animals: 
<ul> 
         <li>Mouse 
         <li>Elephant 
         <li>Python 
</ul> 

参考链接

http://tdcq.iteye.com/blog/748266

posted on   Simle  阅读(239)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示