libs/web: Fixed luci.template
[oweals/luci.git] / libs / web / luasrc / template.lua
index c656cee8d262aed14b3af5548de528aca31ccae1..91977798af8f77679911ae02909951160cc5035a 100644 (file)
@@ -177,24 +177,25 @@ function Template.__init__(self, name)
        local cdir = compiledir .. "/" .. sys.process.info("uid")
        
        -- Compile and build
-       local sourcefile   = viewdir    .. "/" .. name .. ".htm"
+       local sourcefile   = viewdir    .. "/" .. name
        local compiledfile = cdir .. "/" .. _encode_filename(name) .. ".lua"
        local err       
        
        if compiler_mode == "file" then
-               local tplmt = fs.mtime(sourcefile)
+               local tplmt = fs.mtime(sourcefile) or fs.mtime(sourcefile .. ".htm")
                local commt = fs.mtime(compiledfile)
                
                if not fs.mtime(cdir) then
                        fs.mkdir(cdir, true)
                        fs.chmod(fs.dirname(cdir), "a+rxw")
                end
+               
+               assert(tplmt or commt, "No such template: " .. name)
                                
                -- 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
+               if not commt or (commt  and tplmt and commt < tplmt) then
                        local source
-                       source, err = fs.readfile(sourcefile)
+                       source, err = fs.readfile(sourcefile) or fs.readfile(sourcefile .. ".htm")
                        
                        if source then
                                local compiled, err = compile(source)
@@ -214,7 +215,7 @@ function Template.__init__(self, name)
                
        elseif compiler_mode == "memory" then
                local source
-               source, err = fs.readfile(sourcefile)
+               source, err = fs.readfile(sourcefile) or fs.readfile(sourcefile .. ".htm")
                if source then
                        self.template, err = compile(source)
                end
@@ -246,7 +247,7 @@ function Template.render(self, scope)
        local stat, err = util.copcall(self.template)
        if not stat then
                setfenv(self.template, oldfenv)
-               error("Error in template %s: %s" % {self.name, chunk})
+               error("Error in template %s: %s" % {self.name, err})
        end
        
        -- Reset environment