📄 asptemplate.asp
字号:
p_regexp.IgnoreCase = True
p_regexp.Global = True
SetVariable s, inFile
p_regexp.Pattern = p_var_tag_o & s & p_var_tag_c
p_template = p_regexp.Replace(p_template, inFile)
end sub
public property get GetOutput
Dim Matches, match, MatchName
'Replace the variables in the template
p_regexp.IgnoreCase = True
p_regexp.Global = True
p_regexp.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
Set Matches = p_regexp.Execute(p_template)
for each match in Matches
if p_boolSubMatchesAllowed then
MatchName = match.SubMatches(1)
else
MatchName = mid(match.Value,3,Len(match.Value) - 4)
end if
if p_variables_list.Exists(MatchName) then
p_regexp.Pattern = match.Value
p_template = p_regexp.Replace(p_template, p_variables_list.Item(MatchName))
end if
'response.write.write "GetOutput (match): " & match.Value & "<br>"
next
'this removes any block placeholder tags that are left over
p_regexp.Pattern = "__[_a-z0-9]*__"
Set Matches = p_regexp.Execute(p_template)
for each match in Matches
'response.write.write "[[" & match.Value & "]]<br>"
p_regexp.Pattern = match.Value
p_template = p_regexp.Replace(p_template, "")
next
' deal with unknown tags
select case p_unknowns
case "keep"
'do nothing, leave it
case "remove"
'all known matches have been replaced, remove every other match now
p_regexp.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
Set Matches = p_regexp.Execute(p_template)
for each match in Matches
'response.write.Write "Found match: " & match & "<br>"
p_regexp.Pattern = match.Value
p_template = p_regexp.Replace(p_template, "")
next
case "comment"
'all known matches have been replaced, HTML comment every other match
p_regexp.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
Set Matches = p_regexp.Execute(p_template)
for each match in Matches
p_regexp.Pattern = match.Value
if p_boolSubMatchesAllowed then
p_template = p_regexp.Replace(p_template, "<!-- Template variable " & match.Submatches(1) & " undefined -->")
else
p_template = p_regexp.Replace(p_template, "<!-- Template variable " & mid(match.Value,3,len(match) - 4) & " undefined -->")
end if
next
end select
GetOutput = p_template
end property
public sub Parse
Dim parsed
parsed = GetOutput
response.write parsed
end sub
' TODO: if the block foud contains other blocks, it should recursively update all of them without the needing
' of doing this by hand.
public sub UpdateBlock(inBlockName)
Dim Matches, match, aSubMatch
Dim braceStart, braceEnd
p_regexp.IgnoreCase = True
p_regexp.Global = True
p_regexp.Pattern = "<!--\s+BEGIN\s+(" & inBlockName & ")\s+-->([\s\S.]*)<!--\s+END\s+\1\s+-->"
Set Matches = p_regexp.Execute(p_template)
Set match = Matches
for each match in Matches
if p_boolSubMatchesAllowed then
aSubMatch = match.SubMatches(1)
else
braceStart = instr(match,"-->") + 3
braceEnd = instrrev(match,"<!--")
aSubMatch = mid(match,braceStart,braceEnd - braceStart)
end if
'The following check let the user use the same template multiple times
if p_blocks_list.Exists(inBlockName) then
p_blocks_list.Remove(inBlockName)
p_blocks_name_list.Remove(inBlockName)
end if
p_blocks_list.Add inBlockName, aSubMatch
p_blocks_name_list.Add inBlockName, inBlockName
'printInternalTemplate "UpdateBlock: before replace"
p_template = p_regexp.Replace(p_template, "__" & inBlockName & "__")
'printInternalTemplate "UpdateBlock: after replace"
'response.write.write "[[" & server.HTMLEncode(aSubMatch) & "]]<br>"
next
end sub
public sub ParseBlock(inBlockName)
Dim Matches, match, tmp, w, aSubMatch
'debugPrint "Parsing: " + inBlockName
w = GetBlock(inBlockName)
'See if there are any sub-blocks in this block
p_regexp.IgnoreCase = True
p_regexp.Global = True
p_regexp.Pattern = "(__)([_a-z0-9]+)__"
Set Matches = p_regexp.Execute(w)
Set match = Matches
for each match in Matches
if p_boolSubMatchesAllowed then
aSubMatch = match.SubMatches(1)
else
aSubMatch = mid(match.Value,3,len(match) - 4)
end if
'If the sub-block has already been parsed, then replace the block
'identifier with the already parsed text
if p_parsed_blocks_list.Exists(aSubMatch) then
p_regexp.Pattern = "__" & aSubMatch & "__"
w = p_regexp.Replace(w, p_parsed_blocks_list.Item(aSubMatch))
p_parsed_blocks_list.Remove(aSubMatch)
else
'if we are here, that means we are parsing a parent block
'that has a child block that has not yet been parsed. We assume
'that means that the block should remain empty and we therefore
'need to remove the sub-block identifier from the parsed output
'of this block. Otherwise, the sub-block identifiers will be
'replaced in the template on future requests to parse the
'sub-block.
'this removes any block placeholder tags that are left over
p_regexp.Pattern = "__" & aSubMatch & "__"
w = p_regexp.Replace(w, "")
end if
next
'If this block has already been parsed, append the output to the current
'entry in the parsed_blocks_list. Otherwise, create the entry.
if p_parsed_blocks_list.Exists(inBlockName) then
tmp = p_parsed_blocks_list.Item(inBlockName) & w
p_parsed_blocks_list.Remove inBlockName
p_parsed_blocks_list.Add inBlockName, tmp
else
p_parsed_blocks_list.Add inBlockName, w
end if
'Finally, replace the block identifier in the template with the text of
'this block and then append the block identifier in case this block
'is parsed again.
'If the block is not found in the template, we assume that is because
'the block is embedded in a parent block that will be parsed in the future.
'When the parent block is parsed, the content of this block will be included
p_regexp.IgnoreCase = True
p_regexp.Global = True
p_regexp.Pattern = "__" & inBlockName & "__"
Set Matches = p_regexp.Execute(p_template)
Set match = Matches
for each match in Matches
w = GetParsedBlock(inBlockName)
p_regexp.Pattern = "__" & inBlockName & "__"
p_template = p_regexp.Replace(p_template, w & "__" & inBlockName & "__")
next
'printInternalVariables
'printInternalTemplate "ParseBlock: end of function ("+inBlockName+")"
end sub
'Gets the text inside a block, parses and replaces variables, and returns
'the block of text
private property get GetBlock(inToken)
Dim tmp, s
'This routine checks the Dictionary for the text passed to it.
'If it finds a key in the Dictionary it Display the value to the user.
'If not, by default it will display the full Token in the HTML source so that you can debug your templates.
if p_blocks_list.Exists(inToken) then
tmp = p_blocks_list.Item(inToken)
s = ParseBlockVars(tmp)
GetBlock = s
'response.write.write "s: " & s
else
GetBlock = "<!--__" & inToken & "__-->" & VbCrLf
end if
end property
private property get GetParsedBlock(inToken)
Dim tmp, s
'This routine checks the Dictionary for the text passed to it.
'If it finds a key in the Dictionary it Display the value to the user.
'If not, by default it will display the full Token in the HTML source so that you can debug your templates.
if p_blocks_list.Exists(inToken) then
tmp = p_parsed_blocks_list.Item(inToken)
s = ParseBlockVars(tmp)
GetParsedBlock = s
'response.write.write "s: " & s
p_parsed_blocks_list.Remove(inToken)
else
GetParsedBlock = "<!--__" & inToken & "__-->" & VbCrLf
end if
end property
public property get ParseBlockVars(inText)
Dim Matches, match, aSubMatch
p_regexp.IgnoreCase = True
p_regexp.Global = True
p_regexp.Pattern = "(" & p_var_tag_o & ")([^}]+)" & p_var_tag_c
Set Matches = p_regexp.Execute(inText)
for each match in Matches
if p_boolSubMatchesAllowed then
aSubMatch = match.SubMatches(1)
else
aSubMatch = mid(match.Value,3,Len(match.Value) - 4)
end if
if p_variables_list.Exists(aSubMatch) then
p_regexp.Pattern = match.Value
if IsNull(p_variables_list.Item(aSubMatch)) then
inText = p_regexp.Replace(inText, "")
else
inText = p_regexp.Replace(inText, p_variables_list.Item(aSubMatch))
end if
end if
'response.write.write "match.Value: " & match.Value & "<br>"
'response.write.write "in text: " & inText & "<br>"
next
ParseBlockVars = inText
end property
public sub printInternalVariables()
'response.write "<b>p_variables_list:</b>"
'printr p_variables_list
'response.write "<b>p_blocks_list:</b>"
'printr p_blocks_list
'response.write "<b>p_blocks_name_list:</b>"
'printr p_blocks_name_list
response.write "<b>p_parsed_blocks_list:</b>"
printr p_parsed_blocks_list
end sub
public sub printInternalTemplate( sPrefix )
debugPrint sPrefix & "<br /><pre><blockquote>" & Server.HTMLEncode(p_template) & "</blockquote></pre><hr />"
end sub
private sub debugPrint( sText )
response.write sText + "<br />"
end sub
end class
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -