* Remove ffluci.util.__file__ as it relies on debug information
[project/luci.git] / src / ffluci / template.lua
index 18265a4..589f43c 100644 (file)
@@ -31,7 +31,7 @@ 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:
@@ -52,9 +52,9 @@ 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,
+       media      = ffluci.config.main.mediaurlbase,
        write      = io.write,
        include    = function(name) Template(name):render(getfenv(2)) end,      
 }
@@ -108,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
@@ -128,7 +128,7 @@ function render(name, scope, ...)
        scope = scope or getfenv(2)
        local s, t = pcall(Template, name)
        if not s then
-               error("Unable to load template: " .. name)
+               error(t)
        else
                t:render(scope, ...)
        end
@@ -165,7 +165,8 @@ function Template.__init__(self, name)
        
        -- Compile and build
        local sourcefile   = viewdir .. name .. ".htm"
-       local compiledfile = viewdir .. name .. ".lua"  
+       local compiledfile = viewdir .. name .. ".lua"
+       local err       
        
        if compiler_mode == "file" then
                local tplmt = ffluci.fs.mtime(sourcefile)
@@ -174,27 +175,33 @@ function Template.__init__(self, 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
-                       local compiled = compile(ffluci.fs.readfile(sourcefile))
-                       ffluci.fs.writefile(compiledfile, compiled)
-                       self.template = loadstring(compiled)
+                       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
-                       self.template = loadfile(compiledfile)
+                       self.template, err = loadfile(compiledfile)
                end
                
        elseif compiler_mode == "none" then
-               self.template = loadfile(self.compiledfile)
+               self.template, err = loadfile(self.compiledfile)
                
        elseif compiler_mode == "memory" then
-               self.template = loadstring(compile(ffluci.fs.readfile(sourcefile)))
+               local source
+               source, err = ffluci.fs.readfile(sourcefile)
+               if source then
+                       self.template, err = loadstring(compile(source))
+               end
                        
-       else
-               error("Invalid compiler mode: " .. compiler_mode)
-               
        end
        
        -- If we have no valid template throw error, otherwise cache the template
        if not self.template then
-               error("Unable to load template: " .. name)
+               error(err)
        else
                self.cache[name] = self.template
        end