2.0.0-CodeSminth 第一个例子

来源:互联网 发布:淘宝产品推广方法 编辑:程序博客网 时间:2024/06/05 08:52

2.0.1-File-->New-->C# Template

2.0.2加上<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" %> 保证输出文件为UTF-8编码

2.0.3本文的全部代码如下

 

<%-- FileName: Begin2.0.0-TemplateUTF-8.cstName:Author: Description: 添加了<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" %>--%><%-- PartI 模版头<head>作用: (a)指令@ CodeTemplate指明模版信息,(b)指令@ Property指明全局变量 --%><%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" %><%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %><%@ Property Name="SampleStringProperty" Type="System.String" Default="SomeValue"     Optional="True" Category="Strings" Description="This is a sample string property." %><%@ Property Name="SampleBooleanProperty" Type="System.Boolean" Default="True"     Optional="False" Category="Booleans" Description="This is a sample boolean property." %><%--  PartII模版本体<body>嵌入代码实现输出本体--%>My static content here.My dynamic content here: "<%= SampleStringProperty %>"Call a script method: <%= SampleMethod() %><% if (SampleBooleanProperty) { %>My conditional content here.<% } %><%--  PartIII模版嵌入脚本<script >自定义方法函数 --%><script runat="template">// My methods here.public string SampleMethod(){return "Method output.";}</script>


 


 输出为:文本

 

My static content here.My dynamic content here: "SomeValue"Call a script method: Method output.My conditional content here.


 

2.0.4  2.0.3中的模板代码分段解释

(1)  <%--   --%>    为*.cst脚本本身的注释.不会输出

<%--
Name:
Author:
Description: 添加了<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" %>
--%>

(2)CodeTemplate 指令,

属性Language="C#"  脚本语言为C#

属性ResponseEncoding="UTF-8" 输出为UTF-8编码(参见CodeSminth中文乱码解决)

属性TargetLanguage="Text" 输出为语言为 文本Text

<%@ CodeTemplate Language="C#" ResponseEncoding="UTF-8" %><%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="Template description here." %>


(3)Property指令

(a)三个属性相当于初始化String SampleStringProperty=SomeValue;

Property指令的属性Name="SampleStringProperty" 

Property指令的属性Type="System.String"   //必须是.NET 的类型

Property指令的属性 Default="SomeValue"

(b)剩下的是给CodeSminth属性窗口呈现效果的,如图

 

 

 

(4)脚本本体

My dynamic content here:               <%-- (a)没有任何〖<%  %>〗括住的将会直接输出,表示固定文本 --%>
<%= SampleStringProperty %>     <%--(b) 输出变量值 --%>
<%= SampleMethod() %>              <%-- (c)输出函数值 --%>

           <%-- (d)根据SampleBooleanProperty的值判断是否输出 My conditional content here.--%>


<% if (SampleBooleanProperty) { %>
My conditional content here.
<% } %>

 

<%-- (e)脚本的函数SampleMethod(),因为CodeTemplate Language="C#"所以用的是C#语法--%>

<script runat="template">
// My methods here.

public string SampleMethod()
{
 return "Method output.";
}
</script>

 

My static content here.My dynamic content here: "<%= SampleStringProperty %>"Call a script method: <%= SampleMethod() %><% if (SampleBooleanProperty) { %>My conditional content here.<% } %><script runat="template">// My methods here.public string SampleMethod(){return "Method output.";}</script>

 

2.0.5 总结

 

类似.aspx文件,可把文件分为三个区域:  模版头,模版本体,模版嵌入脚本

(1)模版头<head>作用: (a)指明模版信息,(b)全局变量
(2)模版本体<body>嵌入代码实现输出本体
(3)模版嵌入脚本<script >自定义方法函数