webmcp
diff framework/env/slot/render_layout.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children | 2cb27106aa73 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/slot/render_layout.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,48 @@ 1.4 +--[[-- 1.5 +output = -- document/data to be sent to the web browser 1.6 +slot.render_layout() 1.7 + 1.8 +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. 1.9 + 1.10 +--]]-- 1.11 + 1.12 +function slot.render_layout() 1.13 + if slot._current_layout then 1.14 + local layout_file = assert(io.open( 1.15 + encode.file_path( 1.16 + request.get_app_basepath(), 1.17 + 'app', 1.18 + request.get_app_name(), 1.19 + '_layout', 1.20 + slot._current_layout .. '.html' 1.21 + ), 1.22 + 'r' 1.23 + )) 1.24 + local layout = layout_file:read("*a") 1.25 + io.close(layout_file) 1.26 + 1.27 + -- render layout 1.28 + layout = string.gsub(layout, "__BASEURL__/?", request.get_relative_baseurl()) -- TODO: find a better placeholder than __BASEURL__ ? 1.29 + layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOT +([^ ]+) *%-%->', 1.30 + function(slot_ident) 1.31 + if #slot.get_content(slot_ident) > 0 then 1.32 + return '<div class="slot_' .. slot_ident .. '" id="slot_' .. slot_ident .. '">' .. slot.get_content(slot_ident).. '</div>' 1.33 + else 1.34 + return '' 1.35 + end 1.36 + end 1.37 + ) 1.38 + layout = string.gsub(layout, '<!%-%- *WEBMCP +SLOTNODIV +([^ ]+) *%-%->', 1.39 + function(slot_ident) 1.40 + if #slot.get_content(slot_ident) > 0 then 1.41 + return slot.get_content(slot_ident) 1.42 + else 1.43 + return '' 1.44 + end 1.45 + end 1.46 + ) 1.47 + return layout 1.48 + else 1.49 + return slot.get_content("data") 1.50 + end 1.51 +end