在前面的教程CodeSmith 使用教程(3): 自动生成Yii Framework ActiveRecord我们使用了主,从模板来实现了从数据库为Yii Framework生成多个表的ActiveRecord类定义,中CodeSmith项目中通过主模板和从模板的配合可以实现复杂的代码生成过程,主模板和从模板的关系有点类似主程序和子函数的关系。使用主-从模板的基本步骤如下:
- 定义从模板,从模板可以定义属性
- 定义主模板,中主模板中如果要使用从模板,首先需要在主模板中注册从模板,主模板中也也可以定义属性,主模板和从模板中的属性可以通过定义“合并”模式构造最终模板所定义的属性集合。
- 调用主模板,设置主模板和从模板所需的属性生成所需代码
注册子模板
<%@ Register Name="Header" Template="Header.cst"
MergeProperties="True" ExcludeProperties="IncludeMeta" %>
Name:子模板在主模板中的类型名称,在主要模板中可以通过该类型创建子模板的实例
Template: 子模板文件名
MergeProperties: 是否需要把子模板中定义的属性:“合并”到主模板中。缺省为False
ExcludeProperties: 如果子模板的属性合并到主模板中时需要排除的属性列表,以逗号分隔。
子模板复制主模板中的属性
MergeProperties=”True” 可以把从模板中的属性合并到主模板中,如果从模板需要引用主模板的属性,比如主模板中定义了服务器地址,在多个子模板中都需要引用这个属性,此时可以通过复制父模板属性CopyPropertiesTo
来实现:
// instantiate the sub-template
Header header = this.Create<Header>();
// copy all properties with matching name and type to the sub-template instance
this.CopyPropertiesTo(header);
CopyPropertiesTo方法比较主模板中定义的属性和子模板中定义的属性,如果发现从模板中定义的属性和主模板中定义的属性名称类型相同(匹配)则把主模板中属性值复制到子模板中。
设置子模板属性
在主模板中要创建子模板的实例,可以直接通过Create方法
// instantiate the sub-template
Header header = this.Create<Header>();
// include the meta tag
header.IncludeMeta = true;
Create中的Header为注册子模板时Name来定义的类型,通过Create创建子模板的实例后,就直接可以通过该实例的属性来访问子模板中的属性,比如上面代码中IncludeMeta为子模板中定义的一个属性。
<header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;"></header><header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;"><span style="padding: 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; line-height: inherit;">从子模板输出结果</span></header><header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;">创建好子模板的实例,设置好子模板的属性,在主模板中就可以让子模板输出结果,有几种方法可以从子模板输出内容。</header><header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;">第一种是把子模板生成的结果直接插入到主模板中</header><header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;"><pre name="code" class="csharp">// instantiate the sub-template.
Header header = this.Create<Header>();
// render the sub-template to the current output stream.
header.Render(this.Response);
</pre>
<br><span style="color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 26px;">第二种方法是把结果输出到单独的文件中:</span><br></header><header style="padding: 15px 0px 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 21px;"><span style="color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 26px;"></span><pre name="code" class="csharp">// instantiate the sub-template.
Header header = this.Create<Header>();
// render the sub-template to a separate file.
header.RenderToFile("Somefile.txt");
</pre>
<br><span style="color: rgb(85, 85, 85); font-family: 'Times New Roman', Helvetica, serif; font-size: 14px; line-height: 26px;">具体的例子可以参见</span><a href="http://www.imobilebbs.com/wordpress/archives/4196" style="padding: 0px; border: 0px; margin: 0px; font: inherit; vertical-align: baseline; color: rgb(0, 133, 207); text-decoration: initial; line-height: 26px; outline: none; font-family: 'Times New Roman', Helvetica, serif; font-size: 14px;">CodeSmith 使用教程(3): 自动生成Yii Framework ActiveRecord</a></header>
分享到:
相关推荐
codesmith三层代码模板代码模板,codesmith三层代码模板代码模板,codesmith三层代码模板代码模板,codesmith三层代码模板代码模板,codesmith三层代码模板代码模板,codesmith三层代码模板代码模板。
codesmith nettiers-2.3.1.zip codesmith 模板codesmith nettiers-2.3.1.zip codesmith 模板codesmith nettiers-2.3.1.zip codesmith 模板codesmith nettiers-2.3.1.zip codesmith 模板codesmith nettiers-2.3.1.zip...
CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf CodeSmith教程[全].pdf ...
CodeSmith使用教程,开发资料,PDF高清淅版,对CodeSmith熟悉的朋友都知道,CodeSmith是一个基于模板的代码生成工具,它使用类似于ASP.NET的语法来生成代码或文本,本教程将带你掌握CodeSmith的使用方法和原理等...
codesmith 代码模板,帮助开发生成代码使用
CodeSmith安装包已经模板代码和使用教程,原版程序+注册机+教程(包含图文教程),视频教程,三层模板的通用代码生成模板
CodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdfCodeSmith开发使用教程资料 pdf
前言11第 1 章第 1 章概述概述44第 2 章第 2 章编写第一个代码模板编写第一个代码模板11第 3 章第 3 章自动生成Yii Framework Ac
内部包含两个学习文件:CodeSmith.docx和CodeSmith使用手册及实例.pdf
codesmith 3层代码生成模板 codesmith 3层代码生成模板 codesmith 3层代码生成模板
CodeSmith 是一种基于模板的代码生成工具,它使用类似于 ASP.NET 的语法来生成任意类型的代码或文本。与其他许多代码生成工具不同,CodeSmith 不要求您订阅特定的应用程序设计或体系结构。使用 CodeSmith,可以生成...
由于上次上传的codesmith模板(c#),存在一些问题,故而有整理了程序并添加了使用说明。
CodeSmith使用基础教程 非常详细的网页教程
CodeSmith 是一种基于模板的代码生成工具,它使用类似于 ASP.NET 的语法来生成任意类型的代码或文本。与其他许多代码生成工具不同,CodeSmith 不要求您订阅特定的应用程序设计或体系结构。使用 CodeSmith,可以生成...
简单的CodeSmith模板
codesmith使用教程。快速开发会用得到。
CodeSmith模板抽象工厂版,已经可以使用!
CodeSmith 基础教程 word版,比较简单
CodeSmith 是一种基于模板的代码生成工具,它使用类似于ASP.NET的语法来生成任意类型的代码或文本,从而减少重复编码的劳动量,提高效率。与其他许多代码生成工具不同,CodeSmith不要求您订阅特定的应用程序设计或...
CodeSmith 使用手册及实例,初学者学习的很好资料