if not ffooter then ffooter = root .. "/footer.tmpl.html" end
local template = require "resty.template"
+-- NB 25.05.18 template.print = function(s)
+-- NB 25.05.18 ngx.print(s)
+-- NB 25.05.18 end
+ template.nb = self
if self.dev then template.caching(false) end
local content = self:readall(file)
return content
end
+-- function nb.api(url,fmt) return nb:api(url,fmt) end
function nb:api(url,fmt)
local res = ngx.location.capture(url)
+ if res.status >= 500 then ngx.exit(res.status) end
+ if not res.body then return end
+ local json = require('json')
local rows = json.decode(res.body)
+ local html = ''
+ local out = function(s)
+ html = html .. s
+ end
local fct
then
fct = function(r,i)
if i == 1 and (fmt == "table" or fmt == "table-th") then
- ngx.print('<tr>')
+ out('<tr>')
for k,v in pairs(r) do
- ngx.print('<th class="' .. k .. '">' .. k .. '</th>')
+ out('<th class="' .. k .. '">' .. k .. '</th>')
end
- ngx.print('</tr>')
+ out('</tr>')
end
- ngx.print('<tr>')
+ out('<tr>')
for k,v in pairs(r) do
- ngx.print('<td>' .. v .. '</td>')
+ out('<td>' .. v .. '</td>')
end
- ngx.print('<tr>\n')
+ out('<tr>\n')
end
-- Default
for k,v in pairs(r) do
i = i + 1
if i == 1 then
- ngx.print(v)
+ out(v)
else
- ngx.print(string.format(fmt,v))
+ out(string.format(fmt,v))
end
end
- ngx.print('\n')
+ out('\n')
end
end
-- Run
- if fmt == "table" then ngx.print('<table>\n') end
+ if fmt == "table" then out('<table>\n') end
for i,r in pairs(rows) do
fct(r,i)
end
- if fmt == "table" then ngx.print('</table>\n') end
+ if fmt == "table" then out('</table>\n') end
+
+ return html
end
return nb
public $title = 'Dbq';
public $sep_title = ' / ';
+ public $formats = [ 'html','div','csv','xml','json','yaml','sh','sql','php' ];
public $format_cli = 'csv';
public $format_html = 'table';
public $format_html_ua_exp = '/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/';
#public $limits = [];
public $default_limit = '30';
public $limits = ['30','100','500','1000'];
- public $formats = [ 'html','div','csv','xml','json','yaml','sh','sql','php' ];
public $is_html;
# Page
// Write output
if ($this->expires and !preg_match('/^(dump|insert|update|replace|rm|vi)$/',$this->params['action'])) $this->page->expires = $this->expires;
- #if ($this->is_html and $this->params['format'] != 'html') $this->_nopage = true;
+ $this->page->headers_no_cache();
if (empty($this->_nopage)) {
- $this->page->headers_no_cache();
$this->page->js = $this->js;
if ($this->base) {
$this->uri = $path;
$this->uri_params = explode($this->expode_args,$args);
- if (preg_match('/\.(\w+)$/',$path,$m)) {
+ if (preg_match('/\.(\w+(\.html)?)$/',$path,$m)) {
$values[] = $m[1];
$path = substr($path,0,strlen($path)-strlen($m[1])-1);
} else {
// Affect values to objects
if ($format == 'html') $format = $this->format_html;
+ #bye($this->params['format']);
+#bye(preg_match('/^(.*?)\.html$/',$this->params['format']));
+ // Set _nopage
+ if (preg_match('/^(.*?)\.html$/',$this->format_html)) if (preg_match('/^(.*?)\.html$/',$format,$m)) {
+ $format = $m[1];
+ } else {
+ $this->_nopage = true;
+ }
$this->format($format);
$this->method_post_accept_decode();
$this->is_html = strpos($content_type,'html') ? true : false;