ASP 高级模板引擎实现类

代码如下:

Class template

Private c_Char, c_Path, c_FileName, c_Content, c_PageUrl, c_CurrentPage, c_PageStr, ReplacePageStr
    Private TagName

' ***************************************
    '    设置编码
    ' ***************************************
    Public Property Let Char(ByVal Str)
        c_Char = Str
    End Property
    Public Property Get Char
        Char = c_Char
    End Property

' ***************************************
    '    设置模板文件夹路径
    ' ***************************************
    Public Property Let Path(ByVal Str)
        c_Path = Str
    End Property
    Public Property Get Path
        Path = c_Path
    End Property

' ***************************************
    '    设置模板文件名
    ' ***************************************
    Public Property Let FileName(ByVal Str)
        c_FileName = Str
    End Property
    Public Property Get FileName
        FileName = c_FileName
    End Property

' ***************************************
    '    获得模板文件具体路径
    ' ***************************************
    Public Property Get FilePath
        If Len(Path) > 0 Then Path = Replace(Path, "\", "/")
        If Right(Path, 1) <> "/" Then Path = Path & "/"
        FilePath = Path & FileName
    End Property

' ***************************************
    '    设置分页URL
    ' ***************************************
    Public Property Let PageUrl(ByVal Str)
        c_PageUrl = Str
    End Property
    Public Property Get PageUrl
        PageUrl = c_PageUrl
    End Property

' ***************************************
    '    设置分页 当前页
    ' ***************************************
    Public Property Let CurrentPage(ByVal Str)
        c_CurrentPage = Str
    End Property
    Public Property Get CurrentPage
        CurrentPage = c_CurrentPage
    End Property

' ***************************************
    '    输出内容
    ' ***************************************
    Public Property Get Flush
        Response.Write(c_Content)
    End Property

' ***************************************
    '    类初始化
    ' ***************************************
    Private Sub Class_Initialize
        TagName = "pjblog"
        c_Char = "UTF-8"
        ReplacePageStr = Array("", "")
    End Sub

' ***************************************
    '    过滤冲突字符
    ' ***************************************
    Private Function doQuote(ByVal Str)
        doQuote = Replace(Str, Chr(34), """)
    End Function

' ***************************************
    '    类终结
    ' ***************************************
    Private Sub Class_Terminate
    End Sub

' ***************************************
    '    加载文件方法
    ' ***************************************
    Private Function LoadFromFile(ByVal cPath)
        Dim obj
        Set obj = Server.CreateObject("ADODB.Stream")
            With obj
             .Type = 2
                .Mode = 3
                .Open
                .Charset = Char
                .Position = .Size
                .LoadFromFile Server.MapPath(cPath)
                LoadFromFile = .ReadText
                .close
            End With
        Set obj = Nothing
    End Function

' ***********************************************
    '    获取正则匹配对象
    ' ***********************************************
    Public Function GetMatch(ByVal Str, ByVal Rex)
        Dim Reg, Mag
        Set Reg = New RegExp
        With Reg
            .IgnoreCase = True
            .Global = True
            .Pattern = Rex
            Set Mag = .Execute(Str)
            If Mag.Count > 0 Then
                Set GetMatch = Mag
            Else
                Set GetMatch = Server.CreateObject("Scripting.Dictionary")
            End If
        End With
        Set Reg = nothing
    End Function

' ***************************************
    '    打开文档
    ' ***************************************
    Public Sub open
        c_Content = LoadFromFile(FilePath)
    End Sub

' ***************************************
    '    缓冲执行
    ' ***************************************
    Public Sub Buffer
        c_Content = GridView(c_Content)
        Call ExecuteFunction
    End Sub

' ***************************************
    '    GridView
    ' ***************************************
    Private Function GridView(ByVal o_Content)
        Dim Matches, SubMatches, SubText
        Dim Attribute, Content
        Set Matches = GetMatch(o_Content, "\<" & TagName & "\:(\d+?)(.+?)\>([\s\S]+?)<\/" & TagName & "\:\1\>")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                Attribute = SubMatches.SubMatches(1)     ' kocms
                Content = SubMatches.SubMatches(2)     ' <Columns>...</Columns>
                SubText = Process(Attribute, Content)     ' 返回所有过程执行后的结果
                o_Content = Replace(o_Content, SubMatches.value, "<" & SubText(2) & SubText(0) & ">" & SubText(1) & "</" & SubText(2) & ">", 1, -1, 1)                                            ' 替换标签变量
            Next
        End If
        Set Matches = Nothing
        If Len(ReplacePageStr(0)) > 0 Then                ' 判断是否标签变量有值,如果有就替换掉.
            o_Content = Replace(o_Content, ReplacePageStr(0), ReplacePageStr(1), 1, -1, 1)
            ReplacePageStr = Array("", "")                ' 替换后清空该数组变量
        End If
        GridView = o_Content
    End Function

' ***************************************
    '    确定属性
    ' ***************************************
    Private Function Process(ByVal Attribute, ByVal Content)
        Dim Matches, SubMatches, Text
        Dim MatchTag, MatchContent
        Dim datasource, Name, Element, page, id
        datasource = "" : Name = "" : Element = "" : page = 0 : id = ""
        Set Matches = GetMatch(Attribute, "\s(.+?)\=\""(.+?)\""")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                MatchTag = SubMatches.SubMatches(0)                                ' 取得属性名
                MatchContent = SubMatches.SubMatches(1)                            ' 取得属性值
                If Lcase(MatchTag) = "name" Then Name = MatchContent            ' 取得name属性值
                If Lcase(MatchTag) = "datasource" Then datasource = MatchContent' 取得datasource属性值
                If Lcase(MatchTag) = "element" Then Element = MatchContent        ' 取得element属性值
                If Lcase(MatchTag) = "page" Then page = MatchContent            ' 取得page属性值
                If Lcase(MatchTag) = "id" Then id = MatchContent                ' 取得id属性值
            Next
            If Len(Name) > 0 And Len(MatchContent) > 0 Then
                Text = Analysis(datasource, Name, Content, page, id)            ' 执行解析属性
                If Len(datasource) > 0 Then Attribute = Replace(Attribute, "datasource=""" & datasource & """", "")
                If page > 0 Then Attribute = Replace(Attribute, "page=""" & page & """", "")
                Attribute = Replace(Attribute, "name=""" & Name & """", "", 1, -1, 1)
                Attribute = Replace(Attribute, "element=""" & Element & """", "", 1, -1, 1)
                Process = Array(Attribute, Text, Element)
            Else
                Process = Array(Attribute, "", "div")
            End If
        Else
            Process = Array(Attribute, "", "div")
        End If
        Set Matches = Nothing
    End Function

' ***************************************
    '    解析
    ' ***************************************
    Private Function Analysis(ByVal id, ByVal Name, ByVal Content, ByVal page, ByVal PageID)
        Dim Data
        Select Case Lcase(Name)                                                    ' 选择数据源
            Case "loop" Data = DataBind(id, Content, page, PageID)
            Case "for" Data = DataFor(id, Content, page, PageID)
        End Select
        Analysis = Data
    End Function

' ***************************************
    '    绑定数据源
    ' ***************************************
    Private Function DataBind(ByVal id, ByVal Content, ByVal page, ByVal PageID)
        Dim Text, Matches, SubMatches, SubText
        Execute "Text = " & id & "(1)"                                            ' 加载数据源
        Set Matches = GetMatch(Content, "\<Columns\>([\s\S]+)\<\/Columns\>")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                SubText = ItemTemplate(SubMatches.SubMatches(0), Text, page, PageID)' 执行模块替换
                Content = Replace(Content, SubMatches.value, SubText, 1, -1, 1)
            Next
            DataBind = Content
        Else
            DataBind = ""
        End If
        Set Matches = Nothing
    End Function

' ***************************************
    '    匹配模板实例
    ' ***************************************
    Private Function ItemTemplate(ByVal TextTag, ByVal Text, ByVal page, ByVal PageID)
        Dim Matches, SubMatches, SubMatchText
        Dim SecMatch, SecSubMatch
        Dim i, TempText
        Dim TextLen, TextLeft, TextRight
        Set Matches = GetMatch(TextTag, "\<ItemTemplate\>([\s\S]+)\<\/ItemTemplate\>")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                SubMatchText = SubMatches.SubMatches(0)
                ' ---------------------------------------------
                '    循环嵌套开始
                ' ---------------------------------------------
                SubMatchText = GridView(SubMatchText)
                ' ---------------------------------------------
                '    循环嵌套结束
                ' ---------------------------------------------
                If UBound(Text, 1) = 0 Then
                    TempText = ""
                Else
                    TempText = ""
                    ' -----------------------------------------------
                    '    开始分页
                    ' -----------------------------------------------
                    If Len(page) > 0 And page > 0 Then
                        If Len(CurrentPage) = 0 Or CurrentPage = 0 Then CurrentPage = 1
                        TextLen = UBound(Text, 2)
                        TextLeft = (CurrentPage - 1) * page
                        TextRight = CurrentPage * page - 1
                        If TextLeft < 0 Then TextLeft = 0
                        If TextRight > TextLen Then TextRight = TextLen
                        c_PageStr = MultiPage(TextLen + 1, page, CurrentPage, PageUrl, "float:right", "", False)

If Int(Len(c_PageStr)) > 0 Then
                            ReplacePageStr = Array("<page:" & Trim(PageID) & "/>", c_PageStr)
                        Else
                            ReplacePageStr = Array("<page:" & Trim(PageID) & "/>", "")
                        End If
                    Else
                        TextLeft = 0
                        TextRight = UBound(Text, 2)
                    End If

For i = TextLeft To TextRight
                        TempText = TempText & ItemReSec(i, SubMatchText, Text)        ' 加载模板内容
                    Next
                End If
            Next
            ItemTemplate = TempText
        Else
            ItemTemplate = ""
        End If
        Set Matches = Nothing
    End Function

' ***************************************
    '    替换模板字符串
    ' ***************************************
    Private Function ItemReSec(ByVal i, ByVal Text, ByVal Arrays)
        Dim Matches, SubMatches
        Set Matches = GetMatch(Text, "\$(\d+?)")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                Text = Replace(Text, SubMatches.value, doQuote(Arrays(SubMatches.SubMatches(0), i)), 1, -1, 1) '执行替换
            Next
            ItemReSec = Text
        Else
            ItemReSec = ""
        End If
        Set Matches = Nothing
    End Function

' ***************************************
    '    全局变量函数
    ' ***************************************
    Private Sub ExecuteFunction
        Dim Matches, SubMatches, Text, ExeText
        Set Matches = GetMatch(c_Content, "\<function\:([0-9a-zA-Z_\.]*?)\((.*?)\""(.+?)\""(.*?)\)\/\>")
        If Matches.Count > 0 Then
            For Each SubMatches In Matches
                Text = SubMatches.SubMatches(0) & "(" & SubMatches.SubMatches(1) & """" & SubMatches.SubMatches(2) & """" & SubMatches.SubMatches(3) & ")"
                Execute "ExeText=" & Text
                c_Content = Replace(c_Content, SubMatches.value, ExeText, 1, -1, 1)
            Next
        End If
        Set Matches = Nothing
    End Sub

' ***************************************
    '    普通替换全局标签
    ' ***************************************
    Public Property Let Sets(ByVal t, ByVal s)
        Dim SetMatch, Bstr, SetSubMatch
        Set SetMatch = GetMatch(c_Content, "(\<Set\:([0-9a-zA-Z_\.]*?)\(((.*?)" & t & "(.*?))?\)\/\>)")
        If SetMatch.Count > 0 Then
            For Each SetSubMatch In SetMatch
                Execute "Bstr = " & SetSubMatch.SubMatches(1) & "(" & SetSubMatch.SubMatches(3) & """" & s & """" & SetSubMatch.SubMatches(4) & ")"
                c_Content = Replace(c_Content, SetSubMatch.Value, Bstr, 1, -1, 1)
            Next
        End If
        Set SetMatch = Nothing
        Set SetMatch = GetMatch(c_Content, "(\<Set\:" & t & "\/\>)")
        If SetMatch.Count > 0 Then
            For Each SetSubMatch In SetMatch
                c_Content = Replace(c_Content, SetSubMatch.Value, s, 1, -1, 1)
            Next
        End If
        Set SetMatch = Nothing
    End Property

End Class

(0)

相关推荐

  • ASP 高级模板引擎实现类

    复制代码 代码如下: Class template Private c_Char, c_Path, c_FileName, c_Content, c_PageUrl, c_CurrentPage, c_PageStr, ReplacePageStr     Private TagName ' ***************************************     '    设置编码     ' ***************************************    

  • asp.net模板引擎Razor调用外部方法用法实例

    本文实例讲述了asp.net模板引擎Razor调用外部方法用法.分享给大家供大家参考.具体如下: 首先使用Razor的步骤:读取cshtml.解析cshtml同时指定cacheName. 而这个步骤是重复的,为了遵循DRY原则,将这段代码封装为一个RazorHelper()方法 public class RazorHelper { public static string ParseRazor(HttpContext context, string csHtmlVirtualPath, obje

  • asp.net模板引擎Razor中cacheName的问题分析

    本文实例讲述了asp.net模板引擎Razor中cacheName的问题.分享给大家供大家参考.具体如下: 一.为什么使用cacheName 使用cacheName主要是考虑到Razor.Parse()每解析一次都会动态创建一个程序集,如果解析量很大,就会产生很多程序集,大量的程序集调用会造成程序非常慢. 举个例子: 如果编译1000次,编译速度就会很慢. static void Main(string[] args) { string cshtml = File.ReadAllText(@"E

  • ASP.NET Razor模板引擎中输出Html的两种方式

    本文实例讲述了ASP.NET Razor模板引擎中输出Html的两种方式.分享给大家供大家参考,具体如下: Razor中所有的Html都会自动编码,这样就不需要我们手动去编码了(安全),但在需要输出Html时就是已经转义过的Html文本了,如下所示: @{ string thisTest = "<span style=\"color:#f00;\">qubernet</span>"; } @thisTest; 这样在页面输出的文本就是:<

  • asp模板引擎终结者(WEB开发之ASP模式)

    阐述一种全新的ASP模板引擎,实现代码(逻辑)层与HTML(表现)层的分离.这种模板实现方法避免了一 般ASP模板加载模板文件(加载组件)和替换所浪费的资源,实现编译型的模板引擎,提高程序的执行速度和稳定性. 内容:        当前,WEB开发已经变得非常火爆,因为各种应用,已经约来越要求表现层和逻辑层的分离.ASP和HTML夹在一起程序将变得难于维护,可读性也差.在PHP领域,模板引擎已经非常普遍,如phplib,SMARTY,等等.有使用替换方式的,也有编译方式的(SMARTY),它们都

  • asp.net实现在非MVC中使用Razor模板引擎的方法

    本文实例讲述了asp.net实现在非MVC中使用Razor模板引擎的方法.分享给大家供大家参考.具体分析如下: 模板引擎介绍 Razor.Nvelocity.Vtemplate,Razor一般在MVC项目中使用,这里介绍在非MVC项目中的用法. 如何在非MVC中使用Razor模板引擎 借助于开源的RazorEngine,我们可以在非asp.net mvc项目中使用Razor引擎,甚至在控制台.WinForm项目中都可以使用Razor(自己开发代码生成器) 如何使用Razor 环境搭建: ① 添加

  • asp.net使用jquery模板引擎jtemplates呈现表格

    在Asp.net MVC 中,使得我们能够更加自由控制我们所想显示HTML.通常情况下,都要做一下数据列表.那么我们可以手动去拼一个表格出来,但这样有时对于复杂的表格说,那就JS代码比较复杂了.我们可以借助JS下的模板引擎,来实现这一功能.下面要介绍就是JTemplates,它也是基于Jquery的. 复制代码 代码如下: <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><!D

  • PHP模板引擎SMARTY

    用PHP实现MVC开发模式的逻辑层和表示层有多种模板引擎可供选择, 但是官方引擎SMARTY诞生后,选择就有了变化.它的理念和实现都是 相当"前卫"的.本文主要讨论SMARTY之于其他模板引擎的不同特点, 简要介绍了该引擎的安装及使用,并用一个小的测试案例对比了 SMARTY和PHPLIB template的速度和易用性. 一.MVC需要模板 MVC最早是在SmallTalk语言的开发过程中总结出的一种设计模式,MVC分别代 表了"模型"."视图"

  • 简单的自定义php模板引擎

    模板引擎的思想是来源于MVC(Model View Controller)模型,即模型层.视图层.控制器层. 在Web端,模型层为数据库的操作:视图层就是模板,也就是Web前端:Controller就是PHP对数据和请求的各种操作.模板引擎就是为了将视图层和其他层分离开来,使php代码和html代码不会混杂在一起.因为当php代码和html代码混杂在一起时,将使代码的可读性变差,并且代码后期的维护会变得很困难. 大部分的模板引擎原理都差不多,核心就是利用正则表达式解析模板,将约定好的特定的标识语

  • Node.js的Web模板引擎ejs的入门使用教程

    Node 开源模板的选择很多,但推荐像我这样的老人去用 EJS,有 Classic ASP/PHP/JSP 的经验用起 EJS 来的确可以很自然,也就是说,你能够在 <%...%> 块中安排 JavaScript 代码,利用最传统的方式 <%=输出变量%>(另外 <%-输出变量是不会对 & 等符号进行转义的).安装 EJS 命令如下: npm install ejs JS 调用 JS 调用的方法主要有两个: ejs.compile(str, options); //

随机推荐