第五十三章 开发自定义标签 - Using csr %cspQuote Methods
Using csr %cspQuote Methods
%cspQuote
例程定义包含两个不同引用方法的定义。
Quote
QuoteCSP
Quote Method
$$Quote^%cspQuote(line As %String) 复制代码
用引号将输入字符串括起来。
QuoteCSP Method
$$QuoteCSP^%cspQuote(line As %String) 复制代码
用引号将输入字符串括起来,并解析#()#
、##()#
、##''#
、#server
和#url
调用。
创建<grid>
标记以显示表格
本节包含一个名为GridExample
的规则示例,它创建了两个标记,这两个标记在CSP
页面上创建了一个信息表。
<GRID>
tag<GRIDDATA>
tag
Grid Rule Definition
<csr:rule name="GridExample" match="GRID"> <csr:action> <script language =Cache runat=compiler> Set maxrows=##this.GetAttribute("COLS") Set maxcols=##this.GetAttribute("ROWS") Do ..WriteText("<TABLE>",1) Set GRIDDATA="" ;Get Grid Data Set count=##this.Children.Count() For i=1:1:count { Set el=##this.Children.GetAt(i) Set tagname=el.TagName If tagname="GRIDDATA" { Set value=el.GetAttribute("VALUE") Set col=el.GetAttribute("COL") Set row=el.GetAttribute("ROW") Set GRIDDATA(row,col)=value } } ; Write Grid Elements For row=1:1:maxrows { Do ..WriteText("<TR>",1) For col=1:1:maxcols { Set d=$G(GRIDDATA(row,col)) Do ..WriteCSPText("<TD>"_d_"</TD>",1) } } Do ..WriteText("</TR>",1) Do ..WriteText("</TABLE>",1) </SCRIPT> </csr:action> </csr:rule> 复制代码
虽然<GRIDDATA>
属性在<GRID>
的规则定义中被处理,但仍然需要一个空规则来实例化<GRIDDATA>
标记:
<csr:rule name="GridDataExample" match="/GRID/GRIDDATA"> <csr:description> This purpose of this empty rule is to instantiate the GRIDDATA tag into the Document Object Model. </csr:description> <csr:action> <csr:default> </csr:action> </csr:rule> 复制代码
Generated Grid Class
上述规则定义编译成以下两个类:
Grid
GridData
Import User Class csr.csp.GridExample Extends %CSP.Rule { Parameter CSRURL = "/csp/user/GRIDEXAMPLE.CSR"; Method CompilerMethod1() [ Language = cache ] { Set maxrows=##this.GetAttribute("COLS") Set maxcols=##this.GetAttribute("ROWS") Do ..WriteText("<TABLE>",1) Set GRIDDATA="" ;Get Grid Data Set count=##this.Children.Count() For i=1:1:count { Set el=##this.Children.GetAt(i) Set tagname=el.TagName If tagname="GRIDDATA" { Set value=el.GetAttribute("VALUE") Set col=el.GetAttribute("COL") Set row=el.GetAttribute("ROW") Set GRIDDATA(row,col)=value } } ; Write Grid Elements For row=1:1:maxrows { Do ..WriteText("<TR>",1) For col=1:1:maxcols { Set d=$G(GRIDDATA(row,col)) Do ..WriteCSPText("<TD>"_d_"</TD>",1) } } Do ..WriteText("</TR>",1) Do ..WriteText("</TABLE>",1) } Method RenderStartTag() As %Status { New element Set element=##this Set %statuscode=$$$OK Do ..CompilerMethod1() Quit:$$$ISERR(%statuscode) %statuscode Quit $$$SKIPCHILDREN } } 复制代码
GridData Class
Import User Class csr.csp.GridDataExample Extends %CSP.Rule { Parameter CSRURL = "/csp/user/GRIDEXAMPLE.CSR"; Method RenderEndTag() As %Status { New element Set element=##this Do ..RenderDefaultEndTag() Quit $$$OK } Method RenderStartTag() As %Status { New element Set element=##this Do ..RenderDefaultStartTag() Quit $$$PROCESSCHILDREN } } 复制代码
Using the Grid Rule
网格规则现在可以用在CSP
页面的正文中:
<html> <head> <title>Grid Example</title> </head> <body> <grid cols="5" rows="5"> <griddata value="Cell-1-1" col="1" row="1"> </griddata> <griddata value="Cell-2-1" col="2" row="1"> </griddata> <griddata value="Cell-2-2" col="2" row="2"> </griddata> <griddata value="Cell-2-3" col="2" row="3"> </griddata> <griddata value="Cell-2-4" col="2" row="4"> </griddata> <griddata value="Cell-2-5" col="2" row="5"> </griddata> <griddata value="Cell-3-1" col="3" row="1"> </griddata> <griddata value="Cell-4-1" col="4" row="1"> </griddata> <griddata value="Cell-4-3" col="4" row="3"> </griddata> <griddata value="Cell-5-1" col="5" row="1"> </griddata> <griddata value="Cell-5-5" col="5" row="5"> </griddata> </grid> </body> </html> 复制代码
Grid Rule Displayed Page
CSP
页面现在显示以下内容:
Cell-1-1 Cell-2-1 Cell-3-1 Cell-4-1 Cell-5-1 Cell-2-2 Cell-2-3 Cell-4-3 Cell-2-4 Cell-2-5 Cell-5-5