Ader TemplateEngine介绍英文原文

finalshell-4.3.10_windows_x64.zip 立即下载

Ader TemplateEngine is a .NET class library (written in C#) for generating text output from source template and input parameters. It can be used in many scenarios: website page building, email generation, xml generation, source code generation, etc. It's idea is based on antlr stringTemplate (http://www.stringtemplate.org/), but the syntax is based on cold fusion language.

Currently only .NET AderTemplateEngine works with .NET 2.0 only. There is a back-port to 1.1 available in downloads at the bottom.
This document is for version 2.1 of the engine. You can see version 1 here.
Ader TemplateEngine is released under GNU General Public License.

Here is a very simple template:

Thank You for your order #order.billFirstName# #order.billLastName#.
<br>
Your Order Total is: #format(order.total, "C")#
<br>
<ad:if test="#order.shipcountry isnot "US"#">
Your order will arrive in 2-3 weeks
<ad:else>
Your order will arrive in 5-7 days
</ad:if>

The templates can have expressions, if/elseif/else statement, foreach statement, for statement, set statement and other templates.

Templates API: There are 2 classes mainly used in Template Engine: Template and TemplateManager.
Template holds a single instance of a template and TemplateManager is used for executing templates.
Easiest way of creating Templates is by using static methods of Template or TemplateManager:

Template template = Template.FromString(string name, string data)
Template template = Template.FromFile(string name, string filename)

then you use it to instantilate TemplateManager.

TemplateManager mngr = new TemplateManager(template);

or even easier:

TemplateManager mngr = TemplateManager.FromFile(filename);
TemplateManager mngr = TemplateManager.FromString(template);

when using FromString method, the string passed contains template code. This method can be used to dynamically generate text without having templates in files.
You use SetValue(string name, object value); to add values that can be used within the templates.
Ex:

mngr.SetValue("customer", new Customer("Tom", "Jackson"));

then you can refer to customer within the template. You can use any type of object for value. When the value of variable is to be output ToString() method will be called.

- Expressions
Expressions are enclosed with # (hash or pound) characters:
ex.
#firstName#

This example will output value of first name. If you need to output # character, just escape it with another #.
ex.
Your SS## is #ssnumber#

Inside of expression block you can output any variable:
#somevar#

access property or field of a variable:
#somestring.Length#

property name is not case senstivie. So you can call: #string.length# or #string.LENGTH#

or call a function:
#trim(somename)#
You can nest property accesses:
#customer.firstname.length#
You can also call methods on any objects:
#firstname.substring(0, 5)#
or
#customer.isValid()#

Version 2.1 also allows you to use array access from indexed variables:
#somearray[3]# - gets 3rd element of array
#hastable["somekey"]# - gets value of "somekey" from hashtable.
You can use array access with any object that has indexer property.

There are several built in functions and additional functions can be easily added. The built in functions are:
equals(obj1, obj2) - invokes equals method on obj1 with obj2 as parameter. Returns boolean value.

notequals(obj1, obj2) - Returns !equals(obj1, obj2). Is equavilant to calling: not(equals(obj1, obj2))

iseven(num) - tests whether number is an even number

isodd(num) - tests whether number is an odd number

isempty(string) - test whether string has 0 characters. Same as equals(string.Length, 0)

isnotempty(string) - tests whether string has at least 1 character.

isnumber(num) - tests whether num is of numeric type

toupper(string) - converts string to upper case

tolower(string) - converts string to lower case

isdefined(varname) - tests whether variable named varname is defined

ifdefined(varname, value) - returns value if varname is defined. Especiall useful: #ifdefined("name", name)# - will output value of name if it's defined, otherwise will output nothing

len(string) - returns length of string

tolist(collection, property, delim) - will convert collection to string with delim as seperator. If you pass property, the value of the property will be evaluated on each element of collection. If you omit property, then the object itself will be used.
Ex:
suppose you have list as:

	ArrayList list = new ArrayList();
	list.Add("one");
	list.Add("two");
	list.Add("three");
	template.SetValue("mylist", list);

then in your template:
#toList(mylist, " & ")#
the output will be: one & two & three

suppose you have list as:

	list.Add(new Customer("Tom", "Whatever"));
	list.Add(new Customer("Henry", "III"));
	list.Add(new Customer("Tom", "Jackson"));
	template.SetValue("mylist", list);

then in template:
#toList(mylist, "firstName", ",")#
the output will be: Tom,Henry,Tom

isnull(obj) - tests whether obj is null

not(boolvalue) - returns not (!) of boolean value

iif(booleanExpression, iftruevalue, iffalsevalue) - same as booleanExpression ? iftruevalue : iffalsevalue in C#
Ex:
#iif(isodd(i), "bgcolor=yellow", "bgcolor=red")#
will output bgcolor=yellow if i is odd number and bgcolor=red if i is not odd number

format(object, formatstring) - will call ToString(formatstring) on object. Object has to implement IFormattable interface, otherwise ToString() will be called.
Ex:
(suppose total is decimal with value 1208.45)
#format(total, "C")#
will output: $1,208.45

trim(string) - will trim string object

filter(collection, booleanproperty) - will return new List from collection for those objects whose booleanproperty property evaluates to true

gt(obj1, obj2) - will return true if obj1 > obj2 (obj1 and obj2 must implement IComparable. All numeric types do)

lt(obj1, obj2) - will return true if obj1 < obj2 (obj1 and obj2 must implement IComparable. All numeric types do)

compare(obj1, obj2) - will return -1 if obj1 < obj2, 0 is obj1 == obj2, and 1 if obj1 > obj2 (obj1 and obj2 must implement IComparable. All numeric types do)

or(bool1, bool2) - will return true if either bool1 or bool2 are true
ex:
#or(equals(state, "IL"), equals(state, "NY"))# - returns true if state is either IL or NY

and(bool1, bool2) - will return true if both bool1 and bool2 are true

comparenocase(string1, string2) - will do case insenstive comparison of string1 and string2 and return true if they are equal

stripnewlines(string) - will return all /r/n instances and replace them with space

typeof(object) - will return string representation of the type of object. Ex: typeof("hello") return "string". typeof(3) returns int

cint(value) - converts value to integer (internally used Convert.ToInt32 from .net library)

cdouble(value) - converts value to double

cdate(value) - converts value to DateTime type. You can use this function if you want to create datetime objects. Ex: #cdate("2005-5-1")#

createtypereference(type) - you can use this function to create references to static types so that you can access static properties or call methods of a static object. It's most useful when combind with <ad:set tag (explained below)

#createtypereference("System.Math").Round(3.39789)#
#createtypereference("System.Math").PI#
or
<ad:set name="MyMath" value="#createtypereference("System.Math")#" />
#MyMath.Round(3.3)#
#MyMath.PI#



Version 2.1 also adds some operators for common expressions:
is - same as calling function equals. Ex: #obj1 is obj2# will return true if obj1 is equal to obj2.

isnot - same as calling function notequals. Ex: #obj1 isnot obj2#

and - used in if expressions tests (same as && in C#)

or - same as || in c#

lt, lte, gt, gte - less than ("<" in C#), less than or equal ("<="), greater than (">") and greater than or equal (">="). Both operands have to implement IComparable interface. When using numeric types, they have to be of the same type. If you want to compare int to double, you have to convert int to double first using cdbl function.

#varOne lt 3#
#varTwo lte cdbl(3)#
#varThree gt varFour and varFive gte 5.0#

Built In Tags:
IF
You can also conditionally output text based on some expression using special if tag:

<ad:if test="#booleanexpression#">

<ad:elseif test="#bool#">

<ad:else>

</ad:if>

elseif and else are optional. If test of "if" evaluates to true, then block inside of "if" will be output, otherwise elseif will be tested (if exists) and then else.
Ex:

<ad:if test="#cust.country is "US"#">
You are US customer.
<ad:else>
You are from: #cust.country# country.
</ad:if>

If cust.country is "US" then the output will be: You are US customer.

FOREACH
You can loop through collection of elements (any object that implements IEnumerable interface) using FOREACH tag.

<ad:foreach collection="#collection#" var="cust" index="i">
#i#: #cust.lastname#, #cust.firstname#
</ad:foreach>


Suppose customers is array of customer objects: customers = Customer("Tom", "Jackson"), Customer("Mary", "Foo")
The output will be:
1. Jackson, Tom
2. Foo, Mary

During execution, variable name that is passed as var attribute will be assigned with element from the collection. Index attribute can be omitted, and is used to represent index variable for the loop. It starts with 1 and gets increments with each iteration.

FOR
You can use FOR tab to loop through integer values by one.

<ad:for from="1" to="10" index="i">
#i#: #customers[i].name#
</ad:for>



SET
Set tag allows you to set values based on other expressions:
<ad:set name="var" value="#someexpression#" />
After set statement is executed you can use var as if it was a local variable.
It might be useful when accessing complex object values.
Instead of writing:
#customers[i].address.firstname# #customers[i].address.lastname# #customers[i].address.address1#
You can do: lt;ad:set name="add" value="#customers[i].address#" /> #add.firstname# #add.lastname# #add.address1#
It's especially useful with createtypereference function (see above)

Custom Templates:
You can also create your own templates inside of template file that you can call. You do that using template tag:

<ad:template name="ShowCustomer">
#customer.lastname#, #customer.firstname# 
</ad:template>

<ad:showcustomer customer="#cust#" />

You can pass any attributes to the template, and you can use those inside of the template. The template can also access all variables that are defined outside of the template. When calling template you have to put trailing slash at the end, or put closing tag:
<ad:showcustomer />
or
<ad:showcustomer></ad:showcustomer>

The template also received special variable: innerText that is the content of executing the inner elements of calling template.

<ad:template name="bold">
<b>#innerText#</b>
</ad:template>

<ad:bold>#cust.lastname#, #cust.firstname#</ad:bold>

the output will be: <b>Jackson, Tom</b> (if customer is Tom Jackson)

You can also nest those:

<ad:template name="italic">#innerText#</ad:template>

<ad:bold><ad:italic>This will be bold and italic</ad:italic></ad:bold>

You can also invoke templates based on the name using apply tag:

<ad:apply template="#usetemplate#">this is content</ad:apply>

If usetemplate is "bold" then "bold" template will be called.

Templates can be nested inside other template:

<ad:template name="doit">
	<ad:template name="colorme">
	<font color=#color#>#innerText#</font>
	</ad:template>
	
<ad:colorme color="blue">colorize me</ad:colorme>
</ad:template>

colorme template can only be used within doit template.
Templates can also be added programmatically:

TemplateManager mngr = ...;
mngr.AddTemplate(Template.FromString("bold", "<b>#innerText#</b>"));

now bold template can be used anywhere within processing.

Version 2.0 adds ability to create custom tags in C# (or any .net language) that can extend the TemplateManager with additional functionality. Together with the sources is Example 2 which includes 2 custom tags email for sending email and base64 for base64 encoding content.
Once those tags are registered with TemplateManger you can call them like:
<ad:email from="andrew@adersoftware.com" to="someuser@example.com" subject="Hello" server="127.0.0.1"> Hello #customer.firstname# #customer.lastname# </ad:email>

Version 2 also added ITemplateHandler interface for better interaction with template execution. You can than set templateManager's Handler property to a handler, and this handler will be called before and after manager is done processing the template. This handler is also available as this object, and you can access any property or call methods of the handler from within the template. Example 2 includes "MyHandler.cs" as an example on how to use it.

------------------------- Here is a sample based on order confirmation.

class Order
{
	string firstname, lastname, address1, city, state, zip, country;

	public string Address1
	{
		get { return this.address1; }
	}

	public string City
	{
		get { return this.city; }
	}

	public string Country
	{
		get { return this.country; }
	}

	public string Firstname
	{
		get { return this.firstname; }
	}

	public string Lastname
	{
		get { return this.lastname; }
	}

	public string State
	{
		get { return this.state; }
	}

	public string Zip
	{
		get { return this.zip; }
	}
}

Order order = GetOrder();
TemplateManager mngr = TemplateManager.FromFile("order-confirmation.st");
mngr.SetValue("order", order);
System.IO.StringWriter writer = new System.IO.StringWriter();
mngr.Process(writer);

string emailBody = writer.ToString();

-------------------------------------------
order-confirmation.st
-------------------------------------------

<ad:showitem>
#item.sku# - #item.name#<br>
<ad:if test="#equals(item.qty, 1)#">
Price: #format(item.price, "C")#<br>
<ad:else>
You bought #item.qty# items for #format(item.price, "C")# 
	(total: #format(item.total, "C")#)
</ad:if>
</ad:showitem>

#order.firstname# #order.lastname#<br>
#order.address1#<br>
<ad:if test="#isnotempty(order.address2)#">#order.address2#<br></ad:if>
#order.city#, #order.zip# #order.state#
<br>
<table>
<ad:foreach collection="#order.orderitems#" var="orderitem" index="i">
<tr>
	<td>#i#.</td>
	<td bgcolor="#iif(isodd(i), "##DEDEDE", "white")#">
	<ad:showitem item="#orderitem#" />
	</td>
</tr>
</ad:foreach>
</table>
Shipping: #format(order.shipping, "C")#<br>
Taxes: #format(order.tax, "C")#<br>
Order Total: #format(order.total, "C")#<br>

--------------------------------------------
Description of order-confirmation.st
First showitem template is defined which shows a single line item of the order. item is passed as attribute to showitem.
Then address is shown. Note how if is used to conditionally display second line of address with ending <br> tag.
Then each line item of order is looped through using ad:forech tag. iif function is used to color everyother line with #DEDEDE color.

--------------------------------------------------
Example #2 for constructing complex SQL queries:
--------------------------------------------------

string[] cols = new string[]{"id", "name", "email"};
TemplateManager mngr = TemplateManager.FromFile(file);
mngr.SetValue("colums", cols);
mngr.SetValue("tablename", "customer");
string query = mngr.Process();

and the template file is:

select #toList(columns, ",")# from #tablename#
原http://www.adersoftware.com/index.cfm?page=templateEngine2
Ader template 我现在正在研究这个模版引擎,会的人教我一下 立即下载

相关推荐

淘特旅游CMS系统 v4.3 Asp.Net版.rar

淘特旅游网站管理系统2014-4-19 更新说明: 1、栏目分类增加两个属性:缩略图、栏目内容。 2、纠正后台景点管理、线路管理中点击订单显示所有订单的BUG。 3、线路、酒店、景点、租车等模块增加自定义标签和自定义函数支持。 4、会员增加找回密码和后台一键重置密码功能。 5、优化模板显示效果。 6、修改会员注册时帐号可以输入中文的问题。   淘特旅游网站管理系统是我们根据多年CMS开发经验,为面向旅游行业专门定制开发的一套旅游网站整体解决方案。   系统提供旅游线路、酒店、景点、门票、问答、在线预定、信息采集、SEO优化、点评、会员、广告、财务等近百项业务管理模块。系统采用淘特Asp.NetCms为基础架构,信息发布方便灵活,模板 标签机制,前台信息生成静态HTM文件,确保网站在发展状大同时能安全、稳定。   本系统适用对象: 旅行社、旅游公司、酒店旅馆、景区、以及从事旅游相关行业的个人等,以及想运营一个强大的旅游行业网站的公司或个人均可使用。

FinalShell 激活( 4.3.10之前的版本)

下载完成后,双击文件进行安装,除安装路径以外,跟随提示无脑下一步即可。该实现算法基于python程序,所以需要有python环境去运行。网上应该有在线的python程序运行,可以找找看。然后将激活码复制到离线激活中即可。选择适合的版本点击链接进行下载。或者私信我,但不能及时回复。这里就不依次演示了 ~

zneVue 2万+

JumbotCmsV4.2 Beta1源码

JumbotCmsV4.2 Beta1源码 v4.2.0功能更新 1、模板引擎增强,修改自Ader TemplateEngine 2、修改Discuz整合接口模式 3、加入ntalker聊天工具 4、架构重新编写

AderTemplate模版引擎使用分析(二)

Ver2.1还添加了以下操作符: is - 和调用equal方法相同. Ex: #obj1 is obj2# 会返回if obj1 equal obj2 isnot - 和调用notequal方法相同. Ex: #obj1 isnot obj2# and - 相当于c#中的&&操作符 or - 相当于c#中的|| lt, lte, gt, gte - lt(less t...

weixin_30641999的博客 220

国央企创新负责人如何快速识别关键技术成果并推动落地?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

[CTFSHOW]JWT

文章目录前言JWT简介CTFSHOW Web345CTFSHOW Web346前置知识开始做题CTFSHOW Web347CTFSHOW Web348CTFSHOW Web349CTFSHOW Web350前置知识利用WP如何防御 前言 有些师傅向我咨询WP,索性就发了吧,其实早就写好了,只是懒,在github仓库里囤着 JWT简介 可以看看我的Github总结给自己看的,Y4tacker CTFSHOW Web345 首先打开题目,查看源码,接下来访问/admin被跳转回主页啥也没有发生 where is

Y4tacker的博客 6045

单表查询(二)

-- 提取顾客邮件信息的部分内容,起始位置为7,长度为5。 SELECT substr(cemail,7,5) FROM customer; -- 统计所有库存商品的总价值。价值=产品价格*库存数量,求和。 SELECT SUM(pprice*pcount) FROM product; -- 查询数量大于10的订单信息,并按照数量倒序排序。 SELECT * FROM orders WHERE o...

weixin_44512614的博客 667

Trae编程工具Cline插件深度集成与Claude 3.7 API Key高级配置实战

本文详细介绍了如何在Trae编程工具中深度集成Cline插件并配置Claude 3.7 API Key的高级技巧。通过实战案例,展示了如何提升代码补全质量、实现多轮对话记忆及自定义代理设置,特别适合全栈工程师和需要私有化AI服务的团队。文章还涵盖了安装流程、API Key获取方式及企业级部署的最佳实践。

weixin_28829903的博客 436

1. 查询表customer——提取顾客邮件信息的部分内容,起始位置为7,长度为5

SELECT SUBSTR(cemail, 7, 5) FROM customer;

weixin_43939570的博客 766

Vivado FFT IP核配置与实战:FPGA信号处理硬件加速指南

快速傅里叶变换(FFT)是数字信号处理(DSP)中的核心算法,用于将时域信号转换到频域进行分析。其原理基于离散傅里叶变换(DFT),通过蝶形运算的分解与复用,大幅降低了计算复杂度。在工程实践中,软件实现的FFT难以满足高速实时处理的需求,因此硬件加速方案至关重要。FPGA凭借其并行架构和可定制性,成为实现高性能FFT的理想平台。通过集成专用的FFT IP核,如Vivado中的Pipelined Streaming I/O架构,可以构建流水线化的计算引擎,实现极低延迟和高吞吐量的连续数据流处理。这种硬件方案特

weixin_42536863的博客 646

Ader Template Engine exstact

from http://www.codeproject.com/Articles/8141/Ader-Template-Engine, 中文参考boke:http://www.cnblogs.com/caicaihui/archive/2008/07/07/1237756.html Ader TemplateEngine is a .NET class library (written in ...

weixin_30566149的博客 168

光学设计进阶:Zemax中的坐标断点与离轴系统优化

本文深入解析了Zemax中坐标断点的核心原理与实战应用,它是实现离轴光学系统设计的关键。文章通过比喻和实例,详细阐述了如何利用坐标断点作为“方向盘”来精确控制光学元件的偏心和倾斜,实现“前断后接”的局部坐标系变换,从而构建并优化无中心遮拦的离轴反射系统,避免光线失控。

3a9bq4r8t2y的博客 525

介绍TemplateEngine

***************图书推荐*************************************************************************************

TonyWang的专栏 1万+

OpenDataLab 大模型训练数据集下载记录

大模型 训练数据集

u013558123的博客 3341

多智能体记忆架构设计:从分层模型到一致性实践

多智能体记忆架构本质上是一个有状态、分布式、支持遗忘的知识系统。从分层模型入手,结合RBAC隔离、版本向量一致性以及混合存储,可以构建出既灵活又健壮的生产级方案。可微分记忆:让记忆的读写操作嵌入端到端训练。基于大模型的记忆压缩:用LLM自动生成情景摘要,取代规则式遗忘。联邦记忆:不同组织之间的多智能体系统在不共享原始数据的情况下协同记忆。希望本文能为你的多智能体项目提供一块坚实的基石。欢迎在评论区分享你在实践中遇到的记忆架构难题。

Ven%的博客 170

SpringBoot整合Thymleaf实现页面静态化

静态化是指把动态生成的HTML页面变为静态内容保存,以后用户的请求到来,直接访问静态页面,不再经过服务的渲染。而静态的HTML页面可以部署在nginx中,从而大大提高并发能力,减小tomcat压力。ThymeleafContext:运行上下文:模板解析器:模板引擎ContextThymeleaf模板中的上下文: 用来保存模型数据,当模板引擎渲染时,可以从Context上下文中获取数据用于渲染。当与结合使用时,我们放入Model的数据就会被处理到Context,作为模板渲染的数据使用。

haidi8 1444

OpenClaw + Antigravity + 飞书完整部署教程

在 Linux 服务器上部署 OpenClaw,使用 Antigravity 本地代理 AI 模型,并接入飞书实现 24 小时在线 AI 助手。

Maynor的博客 1788

模版引擎AderTemplate源代码分析笔记

概述 AderTemplate是一个小型的模板引擎。无论是拿来直接使用还是用来研究模板引擎实现方式,都是一个不错的选择。本文尝试对其源代码做一些分析。 数据流程 AderTemplate的数据处理流程可以总结为: 模版文件 -> 模版分析 -> Template对象 -> 分析处理Template的Element集合 -> 输出目标文本 模版语法 简化描述如下: 1

kwklover 1663

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

AderTemplate模版引擎使用分析

AderTemplate只能运行于.NET2.0之上. 这里有一个简单例子: ThankYouforyourorder#order.billFirstName##order.billLastName#. <br> YourOrderTotalis:#format(order.total,"C")# <br> <ad:iftest="...

weixin_30333885的博客 140

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

ASP.NET MVC 使用AderTemplate模板引擎进行视图显示

ASP.NET MVC 使用AderTemplateEngine模板引擎进行视图显示 1. 创建ArtTemplateViewLocator视图文件定位类,代码如下: namespace ArtLib { class ArtTemplateViewLocator : ViewLocator { public ArtTemplateViewLocator() {...

207

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Ader TemplateEngine 资料汇总

Ader TemplateEngine 资料汇总 原理: http://www.cnblogs.com/kwklover/archive/2007/07/12/815509.html 使用文档: http://www.cnblogs.com/caicaihui/archive/2008/07/07/1237756.html htt...

weixin_30751947的博客 133

templateEngine2 asp.net 开源模板引擎

非常不错的asp.net开源模板引擎原文:http://www.adersoftware.com/index.cfm?page=templateEngine2中文翻译:http://www.ljf.cn/2010/1/Item41375.html源码下载:http://www.adersoftware.com/index.cfm?page=download&dfile=templateengine2.zip类库下载:http://www.adersoftware.com/index.cfm?page=dow

jackfirst86的专栏 906
上一篇: 网站性能越来越差怎么办
下一篇: C#委托2个简单例子
sendling
博客等级 码龄20年 36粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值