webmcp
view framework/env/slot/render_layout.lua @ 568:c43f251262d8
More layout options for ui.paginate{...} and removed doc of non-implemented option
| author | jbe | 
|---|---|
| date | Wed Oct 13 16:48:34 2021 +0200 (2021-10-13) | 
| parents | d89813dd4d92 | 
| children | 
 line source
     1 --[[--
     2 output =             -- document/data to be sent to the web browser
     3 slot.render_layout(
     4   layout_ident       -- if set, selects layout to be used; otherwise layout set by slot.set_layout(...) is used
     5 )
     7 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.
     9 --]]--
    11 function slot.render_layout(layout_ident)
    12   local layout_ident = layout_ident or slot._current_layout
    13   if layout_ident then
    14     local layout_file = assert(io.open(
    15       encode.file_path(
    16         WEBMCP_BASE_PATH,
    17         'app',
    18         WEBMCP_APP_NAME,
    19         '_layout',
    20         layout_ident .. '.html'
    21       ),
    22       'r'
    23     ))
    24     local layout = assert(layout_file:read("*a"))
    25     assert(layout_file:close())
    27     -- render layout
    28     layout = string.gsub(layout, "__BASEURL__/?", request.get_relative_baseurl())  -- TODO: find a better placeholder than __BASEURL__ ?
    29     layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOT +([^ ]+) *%-%->', 
    30       function(slot_ident)
    31         if #slot.get_content(slot_ident) > 0 then
    32           return '<div class="slot_' .. slot_ident .. '" id="slot_' .. slot_ident .. '">' .. slot.get_content(slot_ident).. '</div>'
    33         else
    34           return ''
    35         end
    36       end
    37     )
    38     layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOTNODIV +([^ ]+) *%-%->', 
    39       function(slot_ident)
    40         if #slot.get_content(slot_ident) > 0 then
    41           return slot.get_content(slot_ident)
    42         else
    43           return ''
    44         end
    45       end
    46     )
    47     return layout
    48   else
    49     return slot.get_content("data")
    50   end
    51 end
