|
aspx1
|
2008-10-05 06:34
| 只看楼主
树型|
收藏|
小
中
大
1#
介绍两个Asp的模板引擎 (转帖)
介绍两个Asp的模板引擎Asp模板引擎一直是Asp开发的一个痛,今天发现有人问相关问题。 所以查了一下sf,发现两个Asp模板引擎。这里给出它们的example。 http://sourceforge.net/projects/asptango/ASP-Tango is a pure ASP Template Engine that provides powerfule, extensible and easy to use separation of HTML presentation and business logic. * * * DISCONTINUED * * * KudzuASP is the new Classic ASP project to replace ASP-Tango. asptango_example.asp <%@ LANGUAGE = "VBScript" %> <!-- #include file="asp-tango.asp" --> <!-- nclude file="asp-tango-compat.asp" --> <% '---------------------------------------------------------------------- ' Module : asptango_example.asp - The Example Script ' Author : Andrew F. Friedl @ TriLogic Industries, LLC ' Created : 2005.02.05 ' Revised : 2005.03.23 ' Version : 0.8.1b ' Copyright: 2005 - TriLogic Industries, LLC '----------------------------------------------------------------------
'------------------------------------------------------------ ' Create an Instance of the Template Engine '------------------------------------------------------------ Dim T_ENGINE Set T_ENGINE = New CTangoEngine T_ENGINE.Debug = False 'MakeBackwardCompatible T_ENGINE
'------------------------------------------------------------ ' Globals for this Script '------------------------------------------------------------ Dim IIdx, JIdx, KIdx Dim MainMenu Dim SubMenus, SubMenu
'------------------------------------------------------------ ' Build the main menu '------------------------------------------------------------ Set MainMenu = New Menu MainMenu.Add "No Banner", "asptango_example.asp?banner=false" MainMenu.Add "No Left Bar ", "asptango_example.asp?leftbar=false" MainMenu.Add "Google!", "http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=GGLD,GGLD:2005-16,GGLD:en&q=asp%2Dtango"
T_ENGINE.PutValue "MenuText", "Home" T_ENGINE.PutValue "MenuURL", "asptango_example.asp"
'------------------------------------------------------------ ' Build the SubMenus '------------------------------------------------------------ SubMenus = Array("","","","") For JIdx = 1 To 3 Set SubMenus(JIdx) = New Menu SubMenus(JIdx).Title = "SubMenu " & JIdx For KIdx = 1 to 3 SubMenus(JIdx).Add "SubItem " & KIdx & "," & KIdx, "asptango_example.asp" Next Next
'------------------------------------------------------------ ' Menu Class Definitions (I like to use classes!) '------------------------------------------------------------ Class MenuITem Dim Text, URL Sub Class_Initialize() Text = "": URL = "#" End Sub End Class Class Menu Dim mItems, Title Sub Class_Initialize() mItems = Array("") Title = "Untitled" End Sub public Default Property Get Item(Index) Set Item = mItems(Index) End Property Property Get Count( ) Count = UBound(mItems) End Property Property Get Items() Items = mItems End Property Sub Add( sText, sURL ) Redim Preserve mItems( Ubound( mItems ) + 1 ) Set mItems( Ubound(mItems) ) = New MenuItem mItems( Ubound(mItems) ).Text = sText mItems( Ubound(mItems) ).URL = sURL End Sub End Class
'------------------------------------------------------------ ' Add some static data to the template engine '------------------------------------------------------------ T_ENGINE.PutValue "PageTitle", "ASP-Tango, Divide and Conquer" T_ENGINE.PutValue "HeaderContent", "<b><font size=+1>Welcome to ASP-Tango!</font></b>" T_ENGINE.PutValue "BannerLeft", "ASP-Tango" T_ENGINE.PutValue "BannerRight", "Divide and Conquer Your ASP Troubles" T_ENGINE.PutValue "BodyContent", "This can be anything!"
'------------------------------------------------------------ ' * * PAGE LAYOUT LOGIC * * '------------------------------------------------------------ ShowBanner = True If Request.QueryString("banner") <> "" Then ShowBanner = CBool( Request.QueryString("banner") ) End If 'ASP Tango can evaluate the ShowBanner variable directly from the ASP Code ' where it is defined, but this is a slow process and prone to errors, so to speed ' things up we put the value into the template engine. T_ENGINE.PutValue "ShowBanner", ShowBanner
' same thig for the ShowLeftBar flag... ShowLeftBar = True If Request.QueryString("leftbar") <> "" Then ShowLeftBar = CBool( Request.QueryString("leftbar") ) End If T_ENGINE.PutValue "ShowLeftBar", ShowLeftBar
' Later on we're going to embed content from another asp page on the server, ' and because the page we invoke might be context specific, we show you how ' to do that here. Dim PageToExecute PageToExecute = "asptango_example_exec.asp"
' ASP Tango can also evaluate functions that have been defined ' within your ASP Code, but again, it is slow and prone to error. In ' fact, having the template engine evaluate script defeats the purpose ' of the template engine, which is to seperate ASP code and layout. Function ShowCopyright() ShowCopyRight = Not CBool( Request.QueryString("copy") ) End Function
' To see if ASP Tango properly evaluates the function above, you can ' comment out the following line of code. T_ENGINE.PutValue "ShowCopyRight", ShowCopyright()
' ' * * * ITERATORS * * * ' ' Prior to version 0.9 ASP Tango used callbacks to handle iteration ' across blocks of templated content. While functional it was slow ' and difficult to program. ' ' ASP Tango now has iterators! Iterators are special classes that ' alloow the template engine to iteratore over blocks of template ' quickly and easily. In fact ASP Tango has 5 pre-defined iterators ' to make the process simple: ' ' + CIterateOverI ' + CIterateOverJ ' + CIterateOverK ' + CIterateERR ' + CIterateNIL ' ' The first four invoke predefined methods directly from your ' ASP Code, which is much faster than the old callback methodology. ' While the older ForEach and ForArray methods may appear to be ' easier to use, they were a constant source ot trouble for many people ' and they were so slow that it is better to be without them. If you feel ' that you cannot live without them, well, they are in the compatibility ' file for you to use. ' ' For this example I'm going to use one the predefined iterators ' CIteratoeOverI. To do that I need to define ONLY two compatible ' functions in my code - GoFirstI and GoNextI. I almost always ' write three though - like this:
'------------------------------------------------------------ ' MAIN MENU - ITERATOR METHODS '------------------------------------------------------------ Function GoFirstI( vNode ) IIdx = 1 GoFirstI = SetDataI( vNode ) End Function Function GoNextI( vNode ) IIdx = IIdx + 1 GoNextI = SetDataI( vNode ) End Function Function SetDataI( vNode ) If IIdx < 1 Or IIdx > MainMenu.Count Then SetDataI = False Exit Function End If Dim oItem: Set oItem = MainMenu(IIdx) vNode.Engine.PutValue "MenuText", oItem.Text vNode.Engine.PutValue "MenuURL", oItem.URL SetDataI = True End Function
' ' All that remains now is to install Now we'll install the interator that will invoke the methods above ' T_ENGINE.PutIterator "MainMenu", new CIterateOverI
'------------------------------------------------------------ ' SUB MENUS - ITERATOR '------------------------------------------------------------ Function GoFirstJ( vNode ) JIdx = 1 GoFirstJ = SetDataJ( vNode ) End Function Function GoNextJ( vNode ) JIdx = JIdx + 1 GoNextJ = SetDataJ( vNode ) End Function Function SetDataJ( vNode ) If JIdx < 1 Or JIdx > UBound(SubMenus) Then SetDataJ = False Exit Function End If Set SubMenu = SubMenus(JIdx) vNode.Engine.PutValue "SubMenuTitle", SubMenu.Title SetDataJ = True End Function
'------------------------------------------------------------ ' SUB MENU ITEMS - ITERATOR '------------------------------------------------------------ Function GoFirstK( vNode ) KIdx = 1 GoFirstK = SetDataK( vNode ) End Function Function GoNextK( vNode ) KIdx = KIdx + 1 GoNextK = SetDataK( vNode ) End Function Function SetDataK( vNode ) If KIdx < 1 Or KIdx > SubMenu.Count Then SetDataK = False Exit Function End If Dim oItem: Set oItem = SubMenu(KIdx) vNode.Engine.PutValue "SubMenuText", oItem.Text vNode.Engine.PutValue "SubMenuURL", oItem.URL SetDataK = True End Function
'------------------------------------------------------------ ' Instal Sub Menu Iterators '------------------------------------------------------------ T_ENGINE.PutIterator "SubMenus", new CIterateOverJ T_ENGINE.PutIterator "SubMenuItems", new CIterateOverK
' ' User Defined Directive - This custom directive will be invoked ' by the template engine because the directive appears within the ' template. Notice that we didn't have to modify the engine code ' to get this to work. ' Sub CustomDirective_Add_P1_P2( vNode ) End Sub
'------------------------------------------------------------ ' Define and Install a Custom Tag Handler '------------------------------------------------------------ Class CTHMyHandler_Add_P1_P2 Sub HandleTag( vNode ) Dim myContent ' push a content block on the evaluation stack, not required but ' generally a good idea. True causes the start tag to be writtn to ' the output. vNode.StackPush True If vNode.ParamCount < 2 Then ' leave and error message vNode.AppendError("|Integer|Integer") Else ' evaluate child content (see below) vNode.EvalNodes ' append some custome content myContent = vNode.ParamItem(1) & " + " myContent = myContent & vNode.ParamItem(2) & " = " myContent = myContent & CStr( CLng(vNode.ParamItem(1)) + CLng(vNode.ParamItem(2)) ) vNode.AppendContent "<b><i>" & myContent & "</i></b>" End If ' pop your content from the stack.. this causes your content to be ' appended to a lower level of the evaluation stack, again True causes ' the end tag (if any) to be written to output. vNode.StackPop True End Sub End Class
T_ENGINE.SetHandler "Add_P1_P2", New CTHMyHandler_Add_P1_P2
'------------------------------------------------------------ ' ASP Tango - Template Processing Invoked '------------------------------------------------------------ T_ENGINE.ParseFile Server.MapPath( "examples.html" ) T_ENGINE.EvalTemplate Response.End %>asptango_example.html // 也就是模板文件 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" /> <!-- ' Module : asptango_example.html - The Example Template ' Author : Andrew F. Friedl @ TriLogic Industries, LLC ' Created : 2005.02.05 ' Revised : 2006.02.10 ' Version : 0.9.7.c ' Copyright: 2005 - TriLogic Industries, LLC --> <html> <head> <title wrid="Replace|Content|PageTitle">PageTitle</title> <link href="asptango.css" rel="stylesheet" type="text/css"> </head>
<body>
<table class="wrPageTABLE"> <tr> <td wrid="Subst|Content" class="wrPageHeaderTD" colspan="2"> <!-- HEADER CONTENT BEGINS --> <span class="AdminHeaderTitle">ASP-Tango</span><br/> <span class="AdminHeaderDesc">A Template Engine for ASP - {{ASPTango_Version}}</span> <!-- HEADER CONTENT ENDS --> </td> </tr> <tr> <td class="wrMainMenuPane" colspan="2"> <a href="/Default.asp" class="wrMainMenuItem">Home</a> <span wrid="Iterate|Content|MainMenu"> <a wrid="Subst|Tag" href="{{MenuURL}}" class="wrMainMenuItem">{{MenuText}}</a> </span> </td> </tr> <tr> <td class="wrPageMenuTD"> <!-- Site Menu and Copyright BEGIN --> <div class="wrContextMenu"> <table class="wrContextMenu"> <tr><td class="wrContextMenuHeader">Menu</td></tr> <tr wrid="Iterate|Tag|SubMenus"> <td wrid="Iterate|Tag|SubMenuItems" xclass="wrContextMenuItem"> <a wrid="Subst|Tag" href="{{SubMenuURL}}" xclass="wrContextMenuText">{{SubMenuText}}</a> </td> </tr> </table> </div> <!-- Site Menu and Copyright END --> </td> <td class="wrPageContentTD" valign="top" align="center">
<!-- Content Begins Here --> <br /> <span wrid="Execute|C|?PageToExecute"> <blockquote> Content from the Execute directive will show in this area. Because this tag directive causes ASP Tango to flush its output, we can call ANY asp page on the server - NOT just another ASP Tango page! So even in extreme cases when you absolutely HAVE to fine tune the output using ASP code, ASP Tango is there with the features you need. </blockquote> </span> <!-- Content Ends Here --> </td> </tr> </table>
<table bgcolor="whitesmoke" width="100%"> <tr> <!-- CustomDirectiveAddP1P2: This is an example of a user defined directive begin invoked natively by the template engine. By natively we mean that it is called in the same way the stock directives are invoked. See asptango_example.asp for more information. --> <td wrid="Add_P1_P2|2001|99" nowrap> Custom directive to add two numbers. Useless but a good example. </td> </tr> </table> </div> </font>
</body>
</html>
|