webmcp

annotate framework/env/slot/render_layout.lua @ 497:d89813dd4d92

New function request.add_error_handler(...); Allow layout_ident to be passed directly to slot.render_layout(...)
author jbe
date Sun Jul 23 02:51:13 2017 +0200 (2017-07-23)
parents be8ede894624
children
rev   line source
jbe/bsw@0 1 --[[--
jbe@497 2 output = -- document/data to be sent to the web browser
jbe@497 3 slot.render_layout(
jbe@497 4 layout_ident -- if set, selects layout to be used; otherwise layout set by slot.set_layout(...) is used
jbe@497 5 )
jbe/bsw@0 6
jbe/bsw@0 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.
jbe/bsw@0 8
jbe/bsw@0 9 --]]--
jbe/bsw@0 10
jbe@497 11 function slot.render_layout(layout_ident)
jbe@497 12 local layout_ident = layout_ident or slot._current_layout
jbe@497 13 if layout_ident then
jbe/bsw@0 14 local layout_file = assert(io.open(
jbe/bsw@0 15 encode.file_path(
jbe@209 16 WEBMCP_BASE_PATH,
jbe/bsw@0 17 'app',
jbe@209 18 WEBMCP_APP_NAME,
jbe/bsw@0 19 '_layout',
jbe@497 20 layout_ident .. '.html'
jbe/bsw@0 21 ),
jbe/bsw@0 22 'r'
jbe/bsw@0 23 ))
jbe@282 24 local layout = assert(layout_file:read("*a"))
jbe@282 25 assert(layout_file:close())
jbe/bsw@0 26
jbe/bsw@0 27 -- render layout
jbe/bsw@0 28 layout = string.gsub(layout, "__BASEURL__/?", request.get_relative_baseurl()) -- TODO: find a better placeholder than __BASEURL__ ?
jbe/bsw@0 29 layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOT +([^ ]+) *%-%->',
jbe/bsw@0 30 function(slot_ident)
jbe/bsw@0 31 if #slot.get_content(slot_ident) > 0 then
jbe/bsw@0 32 return '<div class="slot_' .. slot_ident .. '" id="slot_' .. slot_ident .. '">' .. slot.get_content(slot_ident).. '</div>'
jbe/bsw@0 33 else
jbe/bsw@0 34 return ''
jbe/bsw@0 35 end
jbe/bsw@0 36 end
jbe/bsw@0 37 )
jbe/bsw@0 38 layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOTNODIV +([^ ]+) *%-%->',
jbe/bsw@0 39 function(slot_ident)
jbe/bsw@0 40 if #slot.get_content(slot_ident) > 0 then
jbe/bsw@0 41 return slot.get_content(slot_ident)
jbe/bsw@0 42 else
jbe/bsw@0 43 return ''
jbe/bsw@0 44 end
jbe/bsw@0 45 end
jbe/bsw@0 46 )
jbe/bsw@0 47 return layout
jbe/bsw@0 48 else
jbe/bsw@0 49 return slot.get_content("data")
jbe/bsw@0 50 end
jbe/bsw@0 51 end

Impressum / About Us