Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

127 Zeilen
3.8KB

  1. <?xml version="1.0"?>
  2. <?component error="true" debug="false"?>
  3. <component>
  4. <registration
  5. description="WscMvc ViewRenderer"
  6. progid="WscMvc.ViewRenderer"
  7. version="1.00"
  8. classid="{4948DF84-5DC6-448A-9F1B-EB596C28842B}">
  9. </registration>
  10. <public>
  11. <method name="Render">
  12. <parameter name="viewsDir"/>
  13. <parameter name="viewName"/>
  14. <parameter name="data"/>
  15. <parameter name="output"/>
  16. </method>
  17. </public>
  18. <script language="VBScript">
  19. <![CDATA[
  20. Option Explicit
  21. ' Loads a separate .html template and substitutes {{Key}} placeholders with
  22. ' HTML-encoded values from a Scripting.Dictionary. Never references ASP
  23. ' intrinsics - viewsDir is a plain path string resolved by the caller (same
  24. ' pattern as RequestContext's logDir). Only text-context HTML encoding is
  25. ' implemented (SPEC SS8); attribute/URL/JS-context encoding is out of scope
  26. ' until a view actually needs it. Template content is never executed as
  27. ' VBScript or evaluated as an expression - substitution is a single literal
  28. ' scan-and-replace pass.
  29. Sub Render(viewsDir, viewName, data, output)
  30. Dim fso, templatePath, template, stream
  31. output = ""
  32. If Not IsValidViewName(viewName) Then
  33. Err.Raise vbObjectError + 1, "WscMvc.ViewRenderer", "Invalid view name"
  34. End If
  35. Set fso = CreateObject("Scripting.FileSystemObject")
  36. templatePath = viewsDir & "\" & viewName & ".html"
  37. If Not fso.FileExists(templatePath) Then
  38. Set fso = Nothing
  39. Err.Raise vbObjectError + 2, "WscMvc.ViewRenderer", "View template not found"
  40. End If
  41. Set stream = fso.OpenTextFile(templatePath, 1) ' 1 = ForReading
  42. template = stream.ReadAll
  43. stream.Close
  44. Set stream = Nothing
  45. Set fso = Nothing
  46. output = ExpandTemplate(template, data)
  47. End Sub
  48. Function IsValidViewName(viewName)
  49. Dim i, c
  50. IsValidViewName = False
  51. If IsNull(viewName) Or IsEmpty(viewName) Then Exit Function
  52. If Len(viewName) = 0 Then Exit Function
  53. For i = 1 To Len(viewName)
  54. c = Mid(viewName, i, 1)
  55. If Not ((c >= "A" And c <= "Z") Or (c >= "a" And c <= "z") Or _
  56. (c >= "0" And c <= "9") Or c = "_" Or c = "-") Then
  57. Exit Function
  58. End If
  59. Next
  60. IsValidViewName = True
  61. End Function
  62. ' Single left-to-right scan for {{Key}} tokens. Unterminated "{{" and a key
  63. ' with no matching dictionary entry are both treated as template-authoring
  64. ' errors (Err.Raise) rather than emitted as literal text, so rendered output
  65. ' is always fully resolved - never partially-substituted markup.
  66. Function ExpandTemplate(template, data)
  67. Dim result, pos, openPos, closePos, key, hasKey
  68. result = ""
  69. pos = 1
  70. Do
  71. openPos = InStr(pos, template, "{{")
  72. If openPos = 0 Then
  73. result = result & Mid(template, pos)
  74. Exit Do
  75. End If
  76. result = result & Mid(template, pos, openPos - pos)
  77. closePos = InStr(openPos + 2, template, "}}")
  78. If closePos = 0 Then
  79. Err.Raise vbObjectError + 3, "WscMvc.ViewRenderer", "Unterminated view placeholder"
  80. End If
  81. key = Trim(Mid(template, openPos + 2, closePos - openPos - 2))
  82. hasKey = False
  83. If Not data Is Nothing Then
  84. If data.Exists(key) Then hasKey = True
  85. End If
  86. If Not hasKey Then
  87. Err.Raise vbObjectError + 4, "WscMvc.ViewRenderer", "Unresolved view placeholder"
  88. End If
  89. result = result & HtmlEncode(CStr(data(key)))
  90. pos = closePos + 2
  91. Loop
  92. ExpandTemplate = result
  93. End Function
  94. Function HtmlEncode(s)
  95. Dim r
  96. r = s
  97. r = Replace(r, "&", "&amp;")
  98. r = Replace(r, "<", "&lt;")
  99. r = Replace(r, ">", "&gt;")
  100. r = Replace(r, """", "&quot;")
  101. r = Replace(r, "'", "&#39;")
  102. HtmlEncode = r
  103. End Function
  104. ]]>
  105. </script>
  106. </component>

Powered by TurnKey Linux.