2
0

template.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local util = require "luci.util"
  4. local config = require "luci.config"
  5. local tparser = require "luci.template.parser"
  6. local tostring, pairs, loadstring = tostring, pairs, loadstring
  7. local setmetatable, loadfile = setmetatable, loadfile
  8. local getfenv, setfenv, rawget = getfenv, setfenv, rawget
  9. local assert, type, error = assert, type, error
  10. --- LuCI template library.
  11. module "luci.template"
  12. config.template = config.template or {}
  13. viewdir = config.template.viewdir or util.libpath() .. "/view"
  14. -- Define the namespace for template modules
  15. context = util.threadlocal()
  16. --- Render a certain template.
  17. -- @param name Template name
  18. -- @param scope Scope to assign to template (optional)
  19. function render(name, scope)
  20. return Template(name):render(scope or getfenv(2))
  21. end
  22. --- Render a template from a string.
  23. -- @param template Template string
  24. -- @param scope Scope to assign to template (optional)
  25. function render_string(template, scope)
  26. return Template(nil, template):render(scope or getfenv(2))
  27. end
  28. -- Template class
  29. Template = util.class()
  30. -- Shared template cache to store templates in to avoid unnecessary reloading
  31. Template.cache = setmetatable({}, {__mode = "v"})
  32. -- Constructor - Reads and compiles the template on-demand
  33. function Template.__init__(self, name, template)
  34. if name then
  35. self.template = self.cache[name]
  36. self.name = name
  37. else
  38. self.name = "[string]"
  39. end
  40. -- Create a new namespace for this template
  41. self.viewns = context.viewns
  42. -- If we have a cached template, skip compiling and loading
  43. if not self.template then
  44. -- Compile template
  45. local err
  46. local sourcefile
  47. if name then
  48. sourcefile = viewdir .. "/" .. name .. ".htm"
  49. self.template, _, err = tparser.parse(sourcefile)
  50. else
  51. sourcefile = "[string]"
  52. self.template, _, err = tparser.parse_string(template)
  53. end
  54. -- If we have no valid template throw error, otherwise cache the template
  55. if not self.template then
  56. error("Failed to load template '" .. self.name .. "'.\n" ..
  57. "Error while parsing template '" .. sourcefile .. "':\n" ..
  58. (err or "Unknown syntax error"))
  59. elseif name then
  60. self.cache[name] = self.template
  61. end
  62. end
  63. end
  64. -- Renders a template
  65. function Template.render(self, scope)
  66. scope = scope or getfenv(2)
  67. -- Put our predefined objects in the scope of the template
  68. setfenv(self.template, setmetatable({}, {__index =
  69. function(tbl, key)
  70. return rawget(tbl, key) or self.viewns[key] or scope[key]
  71. end}))
  72. -- Now finally render the thing
  73. local stat, err = util.copcall(self.template)
  74. if not stat then
  75. error("Failed to execute template '" .. self.name .. "'.\n" ..
  76. "A runtime error occurred: " .. tostring(err or "(nil)"))
  77. end
  78. end