template.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local tparser = require "gluon.web.template.parser"
  5. local util = require "gluon.web.util"
  6. local fs = require "nixio.fs"
  7. local tostring, setmetatable, setfenv, pcall, assert = tostring, setmetatable, setfenv, pcall, assert
  8. module "gluon.web.template"
  9. local viewdir = util.libpath() .. "/view/"
  10. local i18ndir = util.libpath() .. "/i18n/"
  11. function renderer(env)
  12. local ctx = {}
  13. local function render_template(name, template, scope)
  14. scope = scope or {}
  15. local locals = {
  16. renderer = ctx,
  17. translate = ctx.translate,
  18. translatef = ctx.translatef,
  19. _translate = ctx._translate,
  20. include = function(name)
  21. ctx.render(name, scope)
  22. end,
  23. }
  24. setfenv(template, setmetatable({}, {
  25. __index = function(tbl, key)
  26. return scope[key] or env[key] or locals[key]
  27. end
  28. }))
  29. -- Now finally render the thing
  30. local stat, err = pcall(template)
  31. assert(stat, "Failed to execute template '" .. name .. "'.\n" ..
  32. "A runtime error occured: " .. tostring(err or "(nil)"))
  33. end
  34. --- Render a certain template.
  35. -- @param name Template name
  36. -- @param scope Scope to assign to template (optional)
  37. function ctx.render(name, scope)
  38. local sourcefile = viewdir .. name .. ".html"
  39. local template, _, err = tparser.parse(sourcefile)
  40. assert(template, "Failed to load template '" .. name .. "'.\n" ..
  41. "Error while parsing template '" .. sourcefile .. "':\n" ..
  42. (err or "Unknown syntax error"))
  43. render_template(name, template, scope)
  44. end
  45. --- Render a template from a string.
  46. -- @param template Template string
  47. -- @param scope Scope to assign to template (optional)
  48. function ctx.render_string(str, scope)
  49. local template, _, err = tparser.parse_string(str)
  50. assert(template, "Error while parsing template:\n" ..
  51. (err or "Unknown syntax error"))
  52. render_template('(local)', template, scope)
  53. end
  54. function ctx.setlanguage(lang)
  55. lang = lang:gsub("_", "-")
  56. if not lang then return false end
  57. if lang ~= 'en' and not fs.access(i18ndir .. "gluon-web." .. lang .. ".lmo") then
  58. return false
  59. end
  60. return tparser.load_catalog(lang, i18ndir)
  61. end
  62. -- Returns a translated string, or nil if none is found
  63. function ctx._translate(key)
  64. return (tparser.translate(key))
  65. end
  66. -- Returns a translated string, or the original string if none is found
  67. function ctx.translate(key)
  68. return tparser.translate(key) or key
  69. end
  70. function ctx.translatef(key, ...)
  71. local t = ctx.translate(key)
  72. return t:format(...)
  73. end
  74. return ctx
  75. end