annotate framework/env/slot/render_layout.lua @ 212:47ebf4213716
Storage of Moonbridge's HTTP request and request handler options in request environment
 | author | 
 jbe | 
 | date | 
 Sat Jan 10 00:50:44 2015 +0100 (2015-01-10) | 
 | parents | 
 2cb27106aa73  | 
 | children | 
 be8ede894624  | 
 
 | rev | 
   line source | 
| 
jbe/bsw@0
 | 
     1 --[[--
 | 
| 
jbe/bsw@0
 | 
     2 output =              -- document/data to be sent to the web browser
 | 
| 
jbe/bsw@0
 | 
     3 slot.render_layout()
 | 
| 
jbe/bsw@0
 | 
     4 
 | 
| 
jbe/bsw@0
 | 
     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.
 | 
| 
jbe/bsw@0
 | 
     6 
 | 
| 
jbe/bsw@0
 | 
     7 --]]--
 | 
| 
jbe/bsw@0
 | 
     8 
 | 
| 
jbe/bsw@0
 | 
     9 function slot.render_layout()
 | 
| 
jbe/bsw@0
 | 
    10   if slot._current_layout then
 | 
| 
jbe/bsw@0
 | 
    11     local layout_file = assert(io.open(
 | 
| 
jbe/bsw@0
 | 
    12       encode.file_path(
 | 
| 
jbe@209
 | 
    13         WEBMCP_BASE_PATH,
 | 
| 
jbe/bsw@0
 | 
    14         'app',
 | 
| 
jbe@209
 | 
    15         WEBMCP_APP_NAME,
 | 
| 
jbe/bsw@0
 | 
    16         '_layout',
 | 
| 
jbe/bsw@0
 | 
    17         slot._current_layout .. '.html'
 | 
| 
jbe/bsw@0
 | 
    18       ),
 | 
| 
jbe/bsw@0
 | 
    19       'r'
 | 
| 
jbe/bsw@0
 | 
    20     ))
 | 
| 
jbe/bsw@0
 | 
    21     local layout = layout_file:read("*a")
 | 
| 
jbe/bsw@0
 | 
    22     io.close(layout_file)
 | 
| 
jbe/bsw@0
 | 
    23 
 | 
| 
jbe/bsw@0
 | 
    24     -- render layout
 | 
| 
jbe/bsw@0
 | 
    25     layout = string.gsub(layout, "__BASEURL__/?", request.get_relative_baseurl())  -- TODO: find a better placeholder than __BASEURL__ ?
 | 
| 
jbe/bsw@0
 | 
    26     layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOT +([^ ]+) *%-%->', 
 | 
| 
jbe/bsw@0
 | 
    27       function(slot_ident)
 | 
| 
jbe/bsw@0
 | 
    28         if #slot.get_content(slot_ident) > 0 then
 | 
| 
jbe/bsw@0
 | 
    29           return '<div class="slot_' .. slot_ident .. '" id="slot_' .. slot_ident .. '">' .. slot.get_content(slot_ident).. '</div>'
 | 
| 
jbe/bsw@0
 | 
    30         else
 | 
| 
jbe/bsw@0
 | 
    31           return ''
 | 
| 
jbe/bsw@0
 | 
    32         end
 | 
| 
jbe/bsw@0
 | 
    33       end
 | 
| 
jbe/bsw@0
 | 
    34     )
 | 
| 
jbe/bsw@0
 | 
    35     layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOTNODIV +([^ ]+) *%-%->', 
 | 
| 
jbe/bsw@0
 | 
    36       function(slot_ident)
 | 
| 
jbe/bsw@0
 | 
    37         if #slot.get_content(slot_ident) > 0 then
 | 
| 
jbe/bsw@0
 | 
    38           return slot.get_content(slot_ident)
 | 
| 
jbe/bsw@0
 | 
    39         else
 | 
| 
jbe/bsw@0
 | 
    40           return ''
 | 
| 
jbe/bsw@0
 | 
    41         end
 | 
| 
jbe/bsw@0
 | 
    42       end
 | 
| 
jbe/bsw@0
 | 
    43     )
 | 
| 
jbe/bsw@0
 | 
    44     return layout
 | 
| 
jbe/bsw@0
 | 
    45   else
 | 
| 
jbe/bsw@0
 | 
    46     return slot.get_content("data")
 | 
| 
jbe/bsw@0
 | 
    47   end
 | 
| 
jbe/bsw@0
 | 
    48 end
 |