* Remove ffluci.util.__file__ as it relies on debug information
[project/luci.git] / src / ffluci / template.lua
index 3d64571..589f43c 100644 (file)
@@ -31,14 +31,13 @@ require("ffluci.fs")
 require("ffluci.i18n")
 require("ffluci.model.uci")
 
-viewdir = ffluci.fs.dirname(ffluci.util.__file__()) .. "view/"
+viewdir = ffluci.config.path .. "/view/"
 
 
 -- Compile modes:
--- none:       Never compile, only render precompiled
+-- none:       Never compile, only use precompiled data from files
 -- memory:     Always compile, do not save compiled files, ignore precompiled 
--- always:  Same as "memory" but also saves compiled files
--- smart:      Compile on demand, save compiled files, update precompiled
+-- file:       Compile on demand, save compiled files, update precompiled
 compiler_mode = "memory"
 
 
@@ -53,28 +52,15 @@ compiler_enable_bytecode = false
 -- Define the namespace for template modules
 viewns = {
        translate  = ffluci.i18n.translate,
-       config     = ffluci.model.uci.get,
+       config     = function(...) return ffluci.model.uci.get(...) or "" end,
        controller = os.getenv("SCRIPT_NAME"),
-       media      = ffluci.config.mediaurlbase,
-       include    = function(name) return render(name, getfenv(2)) end, 
-       write      = io.write
+       media      = ffluci.config.main.mediaurlbase,
+       write      = io.write,
+       include    = function(name) Template(name):render(getfenv(2)) end,      
 }
 
-
--- Compiles and builds a given template
-function build(template, compiled)     
-       local template = compile(ffluci.fs.readfile(template))
-       
-       if compiled then
-               ffluci.fs.writefile(compiled, template)
-       end
-       
-       return template
-end
-
-
 -- Compiles a given template into an executable Lua module
-function compile(template)
+function compile(template)     
        -- Search all <% %> expressions (remember: Lua table indexes begin with #1)
        local function expr_add(command)
                table.insert(expr, command)
@@ -122,7 +108,7 @@ function compile(template)
                elseif p == "~" then
                        re = sanitize(v):gsub("~(.-)%.(.-)%.(.+)", r_uci)
                elseif p == "=" then
-                       re = r_pexec:format(string.sub(v, 2))
+                       re = r_pexec:format(v:sub(2))
                else
                        re = r_exec:format(v)
                end
@@ -137,53 +123,106 @@ function compile(template)
        return template
 end
 
+-- Oldstyle render shortcut
+function render(name, scope, ...)
+       scope = scope or getfenv(2)
+       local s, t = pcall(Template, name)
+       if not s then
+               error(t)
+       else
+               t:render(scope, ...)
+       end
+end
+
+
+-- Template class
+Template = ffluci.util.class()
+
+-- Shared template cache to store templates in to avoid unnecessary reloading
+Template.cache = {}
+
 
--- Returns and builds the template for "name" depending on the compiler mode
-function get(name)     
-       local templatefile = viewdir .. name .. ".htm"
+-- Constructor - Reads and compiles the template on-demand
+function Template.__init__(self, name) 
+       if self.cache[name] then
+               self.template = self.cache[name]
+       else
+               self.template = nil
+       end
+       
+       -- Create a new namespace for this template
+       self.viewns = {}
+       
+       -- Copy over from general namespace
+       for k, v in pairs(viewns) do
+               self.viewns[k] = v
+       end     
+       
+       -- If we have a cached template, skip compiling and loading
+       if self.template then
+               return
+       end
+       
+       -- Compile and build
+       local sourcefile   = viewdir .. name .. ".htm"
        local compiledfile = viewdir .. name .. ".lua"
-       local template = nil
+       local err       
        
-       if compiler_mode == "smart" then
-               local tplmt = ffluci.fs.mtime(templatefile)
+       if compiler_mode == "file" then
+               local tplmt = ffluci.fs.mtime(sourcefile)
                local commt = ffluci.fs.mtime(compiledfile)
                                
                -- Build if there is no compiled file or if compiled file is outdated
                if ((commt == nil) and not (tplmt == nil))
                or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
-                       template = loadstring(build(templatefile, compiledfile))
+                       local source
+                       source, err = ffluci.fs.readfile(sourcefile)
+                       
+                       if source then
+                               local compiled = compile(source)
+                               ffluci.fs.writefile(compiledfile, compiled)
+                               self.template, err = loadstring(compiled)
+                       end
                else
-                       template = loadfile(compiledfile)
+                       self.template, err = loadfile(compiledfile)
                end
                
        elseif compiler_mode == "none" then
-               template = loadfile(compiledfile)
+               self.template, err = loadfile(self.compiledfile)
                
        elseif compiler_mode == "memory" then
-               template = loadstring(build(templatefile))
-               
-       elseif compiler_mode == "always" then
-               template = loadstring(build(templatefile, compiledfile))
-                               
-       else
-               error("Invalid compiler mode: " .. compiler_mode)
-               
+               local source
+               source, err = ffluci.fs.readfile(sourcefile)
+               if source then
+                       self.template, err = loadstring(compile(source))
+               end
+                       
        end
        
-       return template or error("Unable to load template: " .. name)
+       -- If we have no valid template throw error, otherwise cache the template
+       if not self.template then
+               error(err)
+       else
+               self.cache[name] = self.template
+       end
 end
 
+
 -- Renders a template
-function render(name, scope)
+function Template.render(self, scope)
        scope = scope or getfenv(2)
        
-       -- Our template module
-       local view = get(name)
+       -- Save old environment
+       local oldfenv = getfenv(self.template)
        
        -- Put our predefined objects in the scope of the template
-       ffluci.util.updfenv(view, scope)
-       ffluci.util.updfenv(view, viewns)
+       ffluci.util.resfenv(self.template)
+       ffluci.util.updfenv(self.template, scope)
+       ffluci.util.updfenv(self.template, self.viewns)
        
        -- Now finally render the thing
-       return view()
+       self.template()
+       
+       -- Reset environment
+       setfenv(self.template, oldfenv)
 end