webmcp
view framework/env/slot/render_layout.lua @ 231:2eda136b0394
Replace global environment as soon as possible in mcp.lua
author | jbe |
---|---|
date | Sat Feb 28 23:04:43 2015 +0100 (2015-02-28) |
parents | 2cb27106aa73 |
children | be8ede894624 |
line source
1 --[[--
2 output = -- document/data to be sent to the web browser
3 slot.render_layout()
5 This function returns the selected layout after replacing all slot placeholders with the respective slot contents. If slot.set_layout(...) was called with nil as first argument, then no layout will be used, but only the contents of the slot named "data" are returned.
7 --]]--
9 function slot.render_layout()
10 if slot._current_layout then
11 local layout_file = assert(io.open(
12 encode.file_path(
13 WEBMCP_BASE_PATH,
14 'app',
15 WEBMCP_APP_NAME,
16 '_layout',
17 slot._current_layout .. '.html'
18 ),
19 'r'
20 ))
21 local layout = layout_file:read("*a")
22 io.close(layout_file)
24 -- render layout
25 layout = string.gsub(layout, "__BASEURL__/?", request.get_relative_baseurl()) -- TODO: find a better placeholder than __BASEURL__ ?
26 layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOT +([^ ]+) *%-%->',
27 function(slot_ident)
28 if #slot.get_content(slot_ident) > 0 then
29 return '<div class="slot_' .. slot_ident .. '" id="slot_' .. slot_ident .. '">' .. slot.get_content(slot_ident).. '</div>'
30 else
31 return ''
32 end
33 end
34 )
35 layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOTNODIV +([^ ]+) *%-%->',
36 function(slot_ident)
37 if #slot.get_content(slot_ident) > 0 then
38 return slot.get_content(slot_ident)
39 else
40 return ''
41 end
42 end
43 )
44 return layout
45 else
46 return slot.get_content("data")
47 end
48 end