webmcp
annotate framework/env/slot/render_layout.lua @ 31:1cd9e69b85cb
add execute.load_chunk(args)
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
load_chunk loads the content of a lua file and returns the code.
It can construct the path name easily so you can put helper code in
seperate files for DRYness.
| author | Daniel Poelzleithner <poelzi@poelzi.org> | 
|---|---|
| date | Tue Oct 05 02:34:04 2010 +0200 (2010-10-05) | 
| parents | 9fdfb27f8e67 | 
| children | 2cb27106aa73 | 
| 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/bsw@0 | 13 request.get_app_basepath(), | 
| jbe/bsw@0 | 14 'app', | 
| jbe/bsw@0 | 15 request.get_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 |