|
| 1 | +function PageAQL(view, layout, bindVarsView, bindVarsLayout) |
| 2 | + if (BeansEnv == "development") then |
| 3 | + Views["app/views/layouts/" .. layout .. "/index.html.etlua"] = Etlua.compile(LoadAsset("app/views/layouts/" .. |
| 4 | + layout .. "/index.html.etlua")) |
| 5 | + end |
| 6 | + |
| 7 | + layout = Views["app/views/layouts/" .. layout .. "/index.html.etlua"](bindVarsLayout or {}) |
| 8 | + |
| 9 | + local content |
| 10 | + if view:find("%%") then |
| 11 | + content = layout:gsub("@yield", view:gsub("%%", "%%%%")) |
| 12 | + else |
| 13 | + content = layout:gsub("@yield", view) |
| 14 | + end |
| 15 | + local etag = EncodeBase64(Md5(content)) |
| 16 | + |
| 17 | + if etag == GetHeader("If-None-Match") then |
| 18 | + SetStatus(304) |
| 19 | + else |
| 20 | + SetHeader("Etag", etag) |
| 21 | + Write(content) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | + |
| 26 | +function CreateComponent(component) |
| 27 | + local page_content = "" |
| 28 | + |
| 29 | + if component.component ~= "page" and LoadAsset("app/views/partials/components/" .. component.component .. ".html.etlua") then |
| 30 | + local widget = Partial("components/" .. component.component, { component = component }) |
| 31 | + page_content = page_content .. widget |
| 32 | + end |
| 33 | + return page_content |
| 34 | +end |
| 35 | + |
| 36 | +function HandleAqlPage(aql, use_layout, write) |
| 37 | + if write == nil then write = true end |
| 38 | + if use_layout == nil then use_layout = true end |
| 39 | + |
| 40 | + local data = Adb.Aql(aql) |
| 41 | + |
| 42 | + local layout = "" |
| 43 | + local page_title = "" |
| 44 | + local page_description = "" |
| 45 | + local page_keywords = "" |
| 46 | + local page_author = "" |
| 47 | + local page_canonical = "" |
| 48 | + local page_image = "" |
| 49 | + local page_url = "" |
| 50 | + local page_content = "" |
| 51 | + |
| 52 | + if data.result == nil then |
| 53 | + local error = "<pre class=\"col-span-12\"><code>Error on AQL request : \n" .. EncodeJson(data, { pretty = true }) .."</code></pre>" |
| 54 | + if write then Write(error) end |
| 55 | + return error |
| 56 | + end |
| 57 | + |
| 58 | + for key, item in pairs(data.result[1]) do |
| 59 | + if #item == 0 then |
| 60 | + if item.component == "page" then |
| 61 | + page_title = item.title |
| 62 | + page_description = item.description |
| 63 | + page_keywords = item.keywords |
| 64 | + page_author = item.author |
| 65 | + page_canonical = item.canonical |
| 66 | + page_image = item.image |
| 67 | + page_url = item.url |
| 68 | + else |
| 69 | + page_content = page_content .. CreateComponent(item) |
| 70 | + end |
| 71 | + else |
| 72 | + for _, component in pairs(item) do |
| 73 | + page_content = page_content .. CreateComponent(component) |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + if use_layout then |
| 79 | + PageAQL(page_content, "app", {}, { title = page_title, page_content = page_content }) |
| 80 | + else |
| 81 | + if write then |
| 82 | + Write(page_content) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + return page_content |
| 87 | +end |
| 88 | + |
| 89 | +function DisplayAqlPage(filename) |
| 90 | + local aql = LoadAsset("aqlpages/" .. filename .. ".aql") |
| 91 | + local page_content = HandleAqlPage(aql, false, false) |
| 92 | + |
| 93 | + return page_content |
| 94 | +end |
0 commit comments