[ASP] Templates

Templates [ASP] - ASP - Programmation

Marsh Posté le 15-03-2003 à 14:49:39    

Bonjour,
 
J'ai tenté d'utiliser la classe ASPTemplate présente sur ce site : http://asptemplate.sourceforge.net/
 
Mais lorsque je lance l'exemple fourni dans l'archive zip, le navigateur IE6 patoge dans la semoule :(
 
J'ai installé le serveur IIS de WinXP
 
Par contre il fonctionne bien avec IIS4  
 
Avez-vous le même problème avec IIS5 ?
C'est très important pour moi !  
 
Je pense qu'il doit s'agir d'une mauvaise configuration de IIS5 :(
 
Merci pour vos réponses

Reply

Marsh Posté le 15-03-2003 à 14:49:39   

Reply

Marsh Posté le 15-03-2003 à 23:32:08    

Voici mes fichiers de tests :
 
 
Classe asptemplate.asp
 

Code :
  1. <%
  2. '    ASP Template
  3. '    Copyright (C) 2001/2002 Valerio Santinelli
  4. '
  5. '    This library is free software; you can redistribute it and/or
  6. '    modify it under the terms of the GNU Lesser General Public
  7. '    License as published by the Free Software Foundation; either
  8. '    version 2.1 of the License, or (at your option) any later version.
  9. '
  10. '    This library is distributed in the hope that it will be useful,
  11. '    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. '    Lesser General Public License for more details.
  14. '
  15. '    You should have received a copy of the GNU Lesser General Public
  16. '    License along with this library; if not, write to the Free Software
  17. '    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. '
  19. '   ---------------------------------------------------------------------------
  20. '
  21. ' ASP Template main class file
  22. '
  23. ' Author: Valerio Santinelli <tanis@mediacom.it>
  24. ' $Id: asptemplate.asp,v 1.14 2002/06/23 00:57:56 wodge Exp $
  25. '
  26. Option Explicit
  27. '===============================================================================
  28. ' Name: ASPTemplate Class
  29. ' Purpose: HTML separation class
  30. ' Functions:
  31. '     <functions' list in alphabetical order>
  32. ' Properties:
  33. '     <properties' list in alphabetical order>
  34. ' Methods:
  35. '     <Methods' list in alphabetical order>
  36. ' Author: Valerio Santinelli <tanis@mediacom.it>
  37. ' Start: 2001/01/01
  38. ' Modified: 2001/12/19
  39. '===============================================================================
  40. class ASPTemplate
  41. ' Contains the error objects
  42. private p_error
  43. ' Print error messages?
  44. private p_print_errors
  45. ' What to do with unknown tags (keep, remove or comment)?
  46. private p_unknowns
  47. ' Opening delimiter (usually "{{" )
  48. private p_var_tag_o
  49. ' Closing delimiter (usually "}}" )
  50. private p_var_tag_c
  51. 'private p_start_block_delimiter_o
  52. 'private p_start_block_delimiter_c
  53. 'private p_end_block_delimiter_o
  54. 'private p_end_block_delimiter_c
  55. 'private p_int_block_delimiter
  56. private p_template
  57. private p_variables_list
  58. private p_blocks_list
  59. private p_blocks_name_list
  60. private p_regexp
  61. private p_parsed_blocks_list
  62. private p_boolSubMatchesAllowed
  63. ' Directory containing HTML templates
  64. private p_templates_dir
  65. '===============================================================================
  66. ' Name: class_Initialize
  67. ' Purpose: Constructor
  68. ' Remarks: None
  69. '===============================================================================
  70. private sub class_Initialize
  71.  p_print_errors = FALSE
  72.  p_unknowns = "keep"
  73.  ' Remember that opening and closing tags are being used in regular expressions
  74.  ' and must be explicitly escaped
  75.  p_var_tag_o = "\{\{"
  76.  p_var_tag_c = "\}\}"
  77.  ' Block delimiters are actually disabled and no longer available. Maybe they'll be again
  78.  ' in the future.
  79.  'p_start_block_delimiter_o = "<!-- BEGIN "
  80.  'p_start_block_delimiter_c = " -->"
  81.  'p_end_block_delimiter_o = "<!-- END "
  82.  'p_end_block_delimiter_c = " -->"
  83.  'p_int_block_delimiter = "__"
  84.  p_templates_dir = "templates/"
  85.  set p_variables_list = createobject("Scripting.Dictionary" )
  86.  set p_blocks_list = createobject("Scripting.Dictionary" )
  87.  set p_blocks_name_list = createobject("Scripting.Dictionary" )
  88.  set p_parsed_blocks_list = createobject("Scripting.Dictionary" )
  89.  p_template = ""
  90.  p_boolSubMatchesAllowed = not (ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "." & ScriptEngineBuildVersion < "5.5" )
  91.  Set p_regexp = New RegExp 
  92. end sub
  93. '===============================================================================
  94. ' Name: SetTemplatesDir
  95. ' Input:
  96. '    dir as Variant Directory
  97. ' Output:
  98. ' Purpose: Sets the directory containing html templates
  99. ' Remarks: None
  100. '===============================================================================
  101. public sub SetTemplatesDir(dir)
  102.  p_templates_dir = dir
  103. end sub
  104. '===============================================================================
  105. ' Name: SetTemplate
  106. ' Input:
  107. '    template as Variant String containing the template
  108. ' Output:
  109. ' Purpose: Sets a template passed through a string argument
  110. ' Remarks: None
  111. '===============================================================================
  112. public sub SetTemplate(template)
  113.  p_template = template
  114. end sub
  115. '===============================================================================
  116. ' Name: GetTemplate
  117. ' Input:
  118. ' Output:
  119. '    template as Variant String
  120. ' Purpose: returns template as a string
  121. ' Remarks: None
  122. '===============================================================================
  123. public function GetTemplate
  124.  GetTemplate = p_template
  125. end function
  126. '===============================================================================
  127. ' Name: SetUnknowns
  128. ' Input:
  129. '    action as String containing the action to perform with unrecognized
  130. '    tags in the template
  131. ' Output:
  132. ' Purpose: Sets a variable passed through a string argument
  133. ' Remarks: The action can be one of the following:
  134. '  - 'keep': leave the tags untouched
  135. '  - 'remove': remove the tags from the output
  136. '  - 'comment': mark the tags as HTML comment
  137. '===============================================================================
  138. public sub SetUnknowns(action)
  139.  if (action <> "keep" ) and (action <> "remove" ) and (action <> "comment" ) then
  140.   p_unknowns = "keep"
  141.  else
  142.   p_unknowns = action
  143.  end if
  144. end sub
  145. '===============================================================================
  146. ' Name: SetTemplateFile
  147. ' Input:
  148. '    inFileName as Variant Name of the file to read the template from
  149. ' Output:
  150. ' Purpose: Sets a template given the filename to load the template from
  151. ' Remarks: None
  152. '===============================================================================
  153. public sub SetTemplateFile(inFileName)
  154.  if len(inFileName) > 0 then
  155.   dim FSO, oFile
  156.   set FSO = createobject("Scripting.FileSystemObject" )
  157.   'response.write server.mappath(p_templates_dir & inFileName)
  158.   if FSO.FileExists(server.mappath(p_templates_dir & inFileName)) then
  159.    set oFile = FSO.OpenTextFile(server.mappath(p_templates_dir & inFileName), 1)
  160.    p_template = oFile.ReadAll
  161.    oFile.Close
  162.    set oFile = Nothing
  163.   else
  164.    response.write "<b>ASPTemplate Error: File [" & inFileName & "] does not exists!</b><br>"
  165.   end if
  166.   set FSO = nothing
  167.  else
  168.   response.write "<b>ASPTemplate Error: SetTemplateFile missing filename.</b><br>"
  169.  end if
  170. end sub
  171. '===============================================================================
  172. ' Name: SetVariable
  173. ' Input:
  174. '    s as Variant - Variable name
  175. '    v as Variant - Value
  176. ' Output:
  177. ' Purpose: Sets a variable given it's name and value
  178. ' Remarks: None
  179. '===============================================================================
  180. public sub SetVariable(s, v)
  181.  if p_variables_list.Exists(s) then
  182.   p_variables_list.Remove s
  183.   p_variables_list.Add s, v
  184.  else
  185.   p_variables_list.Add s, v
  186.  end if
  187. end sub
  188. '===============================================================================
  189. ' Name: Append
  190. ' Input:
  191. '    s as Variant - Variable name
  192. '    v as Variant - Value
  193. ' Output:
  194. ' Purpose: Sets a variable appending the new value to the existing one
  195. ' Remarks: None
  196. '===============================================================================
  197. public sub Append(s, v)
  198.  Dim tmp
  199.  if p_variables_list.Exists(s) then
  200.   tmp = p_variables_list.Item(s) & v
  201.   p_variables_list.Remove s
  202.   p_variables_list.Add s, tmp
  203.  else
  204.   p_variables_list.Add s, v
  205.  end if
  206. end sub
  207. '===============================================================================
  208. ' Name: SetVariableFile
  209. ' Input:
  210. '    s as Variant Variable name
  211. '    inFileName as Variant Name of the file to read the value from
  212. ' Output:
  213. ' Purpose: Load a file into a variable's value
  214. ' Remarks: None
  215. '===============================================================================
  216. public sub SetVariableFile(s, inFileName)
  217.  if len(inFileName) > 0 then
  218.   dim FSO, oFile
  219.   set FSO = createobject("Scripting.FileSystemObject" )
  220.   if FSO.FileExists(server.mappath(p_templates_dir & inFileName)) then
  221.    set oFile = FSO.OpenTextFile(server.mappath(p_templates_dir & inFileName))
  222.    ReplaceBlock s, oFile.ReadAll
  223.    oFile.Close
  224.    set oFile = Nothing
  225.   else
  226.    response.write "<b>ASPTemplate Error: File [" & inFileName & "] does not exists!</b><br>"
  227.   end if
  228.   set FSO = nothing
  229.  else
  230.   'Filename was never passed!
  231.  end if
  232. end sub
  233. '===============================================================================
  234. ' Name: ReplaceBlock
  235. ' Input:
  236. '    s as Variant Variable name
  237. '    inFile as Variant Content of the file to place in the template
  238. ' Output:
  239. ' Purpose: Function used by SetVariableFile to load a file and replace it
  240. '          into the template in place of a variable
  241. ' Remarks: None
  242. '===============================================================================
  243. public sub ReplaceBlock(s, inFile)
  244.  p_regexp.IgnoreCase = True
  245.  p_regexp.Global = True
  246.  p_regexp.Pattern = p_var_tag_o & s & p_var_tag_c
  247.  p_template = p_regexp.Replace(p_template, inFile) 
  248. end sub
  249. public property get GetOutput
  250.  Dim Matches, match, MatchName
  251.  p_regexp.IgnoreCase = True
  252.  p_regexp.Global = True
  253.  p_regexp.Pattern = "(" & p_var_tag_o & " )([^}]+)" & p_var_tag_c
  254.  Set Matches = p_regexp.Execute(p_template) 
  255.  for each match in Matches
  256.   if p_boolSubMatchesAllowed then
  257.    MatchName = match.SubMatches(1)
  258.   else
  259.    MatchName = mid(match.Value,3,Len(match.Value) - 4)
  260.   end if
  261.   if p_variables_list.Exists(MatchName) then
  262.    p_regexp.Pattern = match.Value
  263.    p_template = p_regexp.Replace(p_template, p_variables_list.Item(MatchName))
  264.   end if
  265.   'response.write match.Value & "<br>"
  266.  next
  267.  p_regexp.Pattern = "__[_a-z0-9]*__"
  268.  Set Matches = p_regexp.Execute(p_template) 
  269.  for each match in Matches
  270.   'response.write "[[" & match.Value & "]]<br>"
  271.   p_regexp.Pattern = match.Value
  272.   p_template = p_regexp.Replace(p_template, "" )
  273.  next
  274.  ' deal with unknown tags
  275.  select case p_unknowns
  276.   case "keep"
  277.    'do nothing, leave it
  278.   case "remove"
  279.    'all known matches have been replaced, remove every other match now
  280.    p_regexp.Pattern = "(" & p_var_tag_o & " )([^}]+)" & p_var_tag_c
  281.    Set Matches = p_regexp.Execute(p_template) 
  282.    for each match in Matches
  283.     'Response.Write "Found match: " & match & "<br>"
  284.     p_regexp.Pattern = match.Value
  285.     p_template = p_regexp.Replace(p_template, "" )
  286.    next
  287.   case "comment"
  288.    'all known matches have been replaced, HTML comment every other match
  289.    p_regexp.Pattern = "(" & p_var_tag_o & " )([^}]+)" & p_var_tag_c
  290.    Set Matches = p_regexp.Execute(p_template) 
  291.    for each match in Matches
  292.     p_regexp.Pattern = match.Value
  293.     if p_boolSubMatchesAllowed then
  294.      p_template = p_regexp.Replace(p_template, "<!-- Template variable " & match.Submatches(1) & " undefined -->" )
  295.     else
  296.      p_template = p_regexp.Replace(p_template, "<!-- Template variable " & mid(match.Value,3,len(match) - 4) & " undefined -->" )
  297.     end if
  298.    next
  299.  end select
  300.  GetOutput = p_template
  301. end property
  302. public sub Parse
  303.  Dim parsed
  304.  parsed = GetOutput
  305.  response.write parsed
  306. end sub
  307. ' TODO: if the block foud contains other blocks, it should recursively update all of them without the needing
  308. ' of doing this by hand.
  309. public sub UpdateBlock(inBlockName)
  310.  Dim Matches, match, aSubMatch
  311.  Dim braceStart, braceEnd
  312.  p_regexp.IgnoreCase = True
  313.  p_regexp.Global = True
  314.  p_regexp.Pattern = "<!--\s+BEGIN\s+(" & inBlockName & " )\s+-->([\s\S.]*)<!--\s+END\s+\1\s+-->"
  315.  Set Matches = p_regexp.Execute(p_template)
  316.  Set match = Matches
  317.  for each match in Matches
  318.   if p_boolSubMatchesAllowed then
  319.    aSubMatch = match.SubMatches(1)
  320.   else
  321.    braceStart = instr(match,"-->" ) + 3
  322.    braceEnd = instrrev(match,"<!--" )
  323.    aSubMatch = mid(match,braceStart,braceEnd - braceStart)
  324.   end if
  325.   'The following check let the user use the same template multiple times
  326.   if p_blocks_list.Exists(inBlockName) then
  327.    p_blocks_list.Remove(inBlockName)
  328.    p_blocks_name_list.Remove(inBlockName)
  329.   end if
  330.   p_blocks_list.Add inBlockName, aSubMatch
  331.   p_blocks_name_list.Add inBlockName, inBlockName
  332.   p_template = p_regexp.Replace(p_template, "__" & inBlockName & "__" )
  333.   'response.write "[[" & server.HTMLEncode(aSubMatch) & "]]<br>"
  334.  next
  335. end sub
  336. public sub ParseBlock(inBlockName)
  337.  Dim Matches, match, tmp, w, aSubMatch
  338.  w = GetBlock(inBlockName)
  339.  p_regexp.IgnoreCase = True
  340.  p_regexp.Global = True
  341.  p_regexp.Pattern = "(__)([_a-z0-9]+)__"
  342.  Set Matches = p_regexp.Execute(w)
  343.  Set match = Matches
  344.  for each match in Matches
  345.   if p_boolSubMatchesAllowed then
  346.    aSubMatch = match.SubMatches(1)
  347.   else
  348.    aSubMatch = mid(match.Value,3,len(match) - 4)
  349.   end if
  350. '   response.write inBlockName & " - " & Server.HTMLEncode(match.Value) & "<br>"
  351. '   response.write "[[" & Server.HTMLEncode(aSubMatch) & "]]<br>"
  352.   if p_parsed_blocks_list.Exists(aSubMatch) then
  353.    w = p_regexp.Replace(w, p_parsed_blocks_list.Item(aSubMatch) & "__" & aSubMatch &"__" )
  354.    p_parsed_blocks_list.Remove(aSubMatch)
  355.   end if
  356.  next
  357.  if p_parsed_blocks_list.Exists(inBlockName) then
  358.   tmp = p_parsed_blocks_list.Item(inBlockName) & w
  359.   p_parsed_blocks_list.Remove inBlockName
  360.   p_parsed_blocks_list.Add inBlockName, tmp
  361.  else
  362.   p_parsed_blocks_list.Add inBlockName, w
  363.  end if
  364.  p_regexp.IgnoreCase = True
  365.  p_regexp.Global = True
  366.  p_regexp.Pattern = "__" & inBlockName & "__"
  367.  Set Matches = p_regexp.Execute(p_template)
  368.  Set match = Matches
  369.  for each match in Matches
  370.   w = GetParsedBlock(inBlockName)
  371.   'response.write "w:" & w
  372.   p_regexp.Pattern = "__" & inBlockName & "__"
  373.   p_template = p_regexp.Replace(p_template, w & "__" & inBlockName & "__" )
  374.   'response.write "[[" & match.Value & "]]<br>"
  375.   'response.write "[[" & p_regexp.Pattern & "]]<br>"
  376.  next
  377. end sub
  378. private property get GetBlock(inToken)
  379.  Dim tmp, s
  380.  'This routine checks the Dictionary for the text passed to it.
  381.  'If it finds a key in the Dictionary it Display the value to the user.
  382.  'If not, by default it will display the full Token in the HTML source so that you can debug your templates.
  383.  if p_blocks_list.Exists(inToken) then
  384.   tmp = p_blocks_list.Item(inToken)
  385.   s = ParseBlockVars(tmp)
  386.   GetBlock = s
  387.   'response.write "s: " & s
  388.  else
  389.   GetBlock = "<!--__" & inToken & "__-->" & VbCrLf
  390.  end if
  391. end property
  392. private property get GetParsedBlock(inToken)
  393.  Dim tmp, s
  394.  'This routine checks the Dictionary for the text passed to it.
  395.  'If it finds a key in the Dictionary it Display the value to the user.
  396.  'If not, by default it will display the full Token in the HTML source so that you can debug your templates.
  397.  if p_blocks_list.Exists(inToken) then
  398.   tmp = p_parsed_blocks_list.Item(inToken)
  399.   s = ParseBlockVars(tmp)
  400.   GetParsedBlock = s
  401.   'response.write "s: " & s
  402.   p_parsed_blocks_list.Remove(inToken)
  403.  else
  404.   GetParsedBlock = "<!--__" & inToken & "__-->" & VbCrLf
  405.  end if
  406. end property
  407. public property get ParseBlockVars(inText)
  408.  Dim Matches, match, aSubMatch
  409.  p_regexp.IgnoreCase = True
  410.  p_regexp.Global = True
  411.  p_regexp.Pattern = "(" & p_var_tag_o & " )([^}]+)" & p_var_tag_c
  412.  Set Matches = p_regexp.Execute(inText) 
  413.  for each match in Matches
  414.   if p_boolSubMatchesAllowed then
  415.    aSubMatch = match.SubMatches(1)
  416.   else
  417.    aSubMatch = mid(match.Value,3,Len(match.Value) - 4)
  418.   end if
  419.   if p_variables_list.Exists(aSubMatch) then
  420.    p_regexp.Pattern = match.Value
  421.    inText = p_regexp.Replace(inText, p_variables_list.Item(aSubMatch))
  422.   end if
  423.   'response.write match.Value & "<br>"
  424.   'response.write inText & "<br>"
  425.  next
  426.  ParseBlockVars = inText
  427. end property
  428. end class
  429. %>


 
 
 
Fichier de test default.asp
 

Code :
  1. <%
  2. '    ASP Template
  3. '    Copyright (C) 2001/2002 Valerio Santinelli
  4. '
  5. '    This library is free software; you can redistribute it and/or
  6. '    modify it under the terms of the GNU Lesser General Public
  7. '    License as published by the Free Software Foundation; either
  8. '    version 2.1 of the License, or (at your option) any later version.
  9. '
  10. '    This library is distributed in the hope that it will be useful,
  11. '    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. '    Lesser General Public License for more details.
  14. '
  15. '    You should have received a copy of the GNU Lesser General Public
  16. '    License along with this library; if not, write to the Free Software
  17. '    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. '
  19. '   ---------------------------------------------------------------------------
  20. '
  21. ' Main Example File
  22. '
  23. ' Author: Valerio Santinelli <tanis@mediacom.it>
  24. ' $Id: default.asp,v 1.7 2002/02/25 13:57:07 tanis Exp $
  25. '
  26. %>
  27. <!--#INCLUDE FILE="..\asptemplate.asp"-->
  28. <%
  29. 'Declare our template variable
  30. dim t
  31. 'Create the class object
  32. set t = New ASPTemplate
  33. 'Set the main template file
  34. t.SetTemplateFile "layout.html"
  35. 'Add some custom tags to the template
  36. t.SetVariable "title", "ASP Template Example"
  37. 'We add some more text to this tag
  38. t.Append "title", " - Main Page"
  39. 'Load an external file into a tag.
  40. t.SetVariableFile "content", "content.html"
  41. 'Load another external file that will be used to draw some blocks
  42. t.SetVariableFile "menu", "menu.html"
  43. 'Setup the block
  44. t.UpdateBlock "menu_block"
  45. 'Set and parse the block
  46. t.SetVariable "menu_text", "HOME"
  47. t.ParseBlock "menu_block"
  48. 'Do it multiple times
  49. t.SetVariable "menu_text", "NEWS"
  50. t.ParseBlock "menu_block"
  51. t.SetVariable "menu_text", "CREDITS"
  52. t.ParseBlock "menu_block"
  53. 'Store the blocks in their directory service (order is important)
  54. t.UpdateBlock "c_block"
  55. t.UpdateBlock "b_block"
  56. t.UpdateBlock "a_block"
  57. t.SetVariable "inner", "666666"
  58. t.SetVariable "outer", "Outer Block (A)"
  59. t.SetVariable "whatever", "Block C 1"
  60. t.ParseBlock "c_block"
  61. t.SetVariable "whatever", "Block C 2"
  62. t.ParseBlock "c_block"
  63. t.SetVariable "whatever", "Block C 3"
  64. t.ParseBlock "c_block"
  65. t.SetVariable "into_b", "Block B 1"
  66. t.ParseBlock "b_block"
  67. t.SetVariable "whatever", "Block C 1(b)"
  68. t.ParseBlock "c_block"
  69. t.SetVariable "whatever", "Block C 2(b)"
  70. t.ParseBlock "c_block"
  71. t.SetVariable "inner", "999999"
  72. t.SetVariable "into_b", "Block B 2"
  73. t.ParseBlock "b_block"
  74. t.ParseBlock "a_block"
  75. 'Generate the page
  76. t.Parse
  77. 'Destroy our objects
  78. set t = nothing
  79. %>


 
 
Est-ce que quelqu'un pourrait me dire pourquoi ça ne fonctionne pas sous IIS5 ?  
 
J'y perds la tête !

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed