Module: TentSteakFeatures::HtmlView
- Included in:
- HtmlController
- Defined in:
- lib/tent_steak/html.rb
Overview
TentSteak view methods for :html feature. Provides HTML helpers for displaying flash messages, auto-linking ids, and performing ERB substitutions on CSS stylesheets.
Instance Method Summary collapse
-
#add_flash(flash_type, msg) ⇒ Object
Append a new flash message to the given type (
:info,:warn, or:error). -
#display_flash(*flash_types, &block) ⇒ Object
Render all flash messages for all of the given types (
:error,:warn, or:info). -
#flash_clear ⇒ Object
Remove all current flash messages.
-
#get_flash(flash_type = :info) ⇒ Object
Retrieve the flash messages for the given flash type (
:info,:warn, or:error). -
#id_link(value, link_class) ⇒ Object
Format text as a hyperlink to the given Camping Controller.
-
#link_stylesheet(filename, params = nil) ⇒ Object
Loads the given CSS stylesheet via #load_stylesheet and inlines it inside an HTML <style> element.
-
#load_stylesheet(filename, params = nil) ⇒ Object
Loads a parameterized CSS stylesheet.
Instance Method Details
#add_flash(flash_type, msg) ⇒ Object
Append a new flash message to the given type (:info, :warn, or :error).
53 54 55 |
# File 'lib/tent_steak/html.rb', line 53 def add_flash(flash_type, msg) get_flash(flash_type) << msg end |
#display_flash(*flash_types, &block) ⇒ Object
Render all flash messages for all of the given types (:error, :warn, or :info). If no types given, render all types. By default, wraps each message in a <p> element with a class of the flash type. Thus, with the following prep:
add_flash :info, "First info message"
add_flash :warn, "First warn message"
add_flash :warn, "Second warn message"
Calling display_flash with no parameters will render this HTML:
<p class="warn">First warn </p>
<p class="warn">Second warn message</p>
<p class="info">First info </p>
display_flash(:error, :warn) will result in:
<p class="warn">First warn </p>
<p class="warn">Second warn message</p>
To customize the HTML output, pass in a block. The block will be called once with messages for each flash type. For example, to render all messages in a table:
table.flash_table! do
display_flash do |flash_type, msgs|
tr(:class => "#{flash_type}_header") { td flash_type.to_s }
msgs.each { |msg| tr(:class => "#{flash_type}_row") { td msg } }
end
end
The rendered HTML would look something like this:
<table id="flash_table">
<tr class="warn_header"><td>warn</td></tr>
<tr class="warn_row"><td>First warn </td></tr>
<tr class="warn_row"><td>Second warn </td></tr>
<tr class="info_header"><td>info</td></tr>
<tr class="info_row"><td>First info </td></tr>
</table>
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tent_steak/html.rb', line 98 def display_flash(*flash_types, &block) flash_types = [:error, :warn, :info] if flash_types.empty? flash_types.each do |flash_type| msgs = get_flash(flash_type) if block_given? yield(flash_type, msgs) else msgs.each { |msg| p(msg, :class => flash_type) } end end # Make sure we don't return anything to the caller. Otherwise, if nothing # to display, the flash_types array above is passed out and rendered. Ugh. nil end |
#flash_clear ⇒ Object
Remove all current flash messages.
34 35 36 37 38 |
# File 'lib/tent_steak/html.rb', line 34 def flash_clear @flash_error_msgs = [] @flash_warn_msgs = [] @flash_info_msgs = [] end |
#get_flash(flash_type = :info) ⇒ Object
Retrieve the flash messages for the given flash type (:info, :warn, or :error).
42 43 44 45 46 47 48 49 |
# File 'lib/tent_steak/html.rb', line 42 def get_flash(flash_type = :info) case flash_type when :error then @flash_error_msgs ||= [] when :warn then @flash_warn_msgs ||= [] when :info then @flash_info_msgs ||= [] else raise "Unknown flash message type: #{flash_type.inspect}" end end |
#id_link(value, link_class) ⇒ Object
Format text as a hyperlink to the given Camping Controller. The Controller class must have a get() handler that will accept the passed value. For example,
id_link(123456, ShowItem)
would require a Camping class like this:
module MyApp::Controllers
class ShowItem < R '/item/(\d+)'
def get(item_id)
end
end
end
Thus, if the user clicks the generated link (e.g. myapp.com/item/123456), Camping will call MyApp::Controllers::ShowItem.get(123456).
130 131 132 |
# File 'lib/tent_steak/html.rb', line 130 def id_link(value, link_class) (value.nil? || value.blank?) ? " " : (a value, :href => R(link_class, value)) end |
#link_stylesheet(filename, params = nil) ⇒ Object
Loads the given CSS stylesheet via #load_stylesheet and inlines it inside an HTML <style> element. Results in bigger HTML files, but saves a second trip through Camping to render the stylesheet (expensive in CGI mode).
164 165 166 167 |
# File 'lib/tent_steak/html.rb', line 164 def link_stylesheet(filename, params = nil) stylesheet = load_stylesheet(filename, params) style(stylesheet, :type => "text/css", :media => "screen") end |
#load_stylesheet(filename, params = nil) ⇒ Object
Loads a parameterized CSS stylesheet. If a params hash is passed in, the stylesheet will be processed with ERB. Each key/value pair in params is evaluated as key = value and passed into ERB as variables.
Thus, the following invocation
load_stylesheet "my-stylesheet.css", :title_color => "rgb(80,150,25)"
would allow for substitutions in my-stylesheet.css like:
.tabletitle {
background: <%= title_color %>;
}
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/tent_steak/html.rb', line 147 def load_stylesheet(filename, params = nil) stylesheet = File.read(filename) if params.instance_of?(Hash) # Explicitly pass execution context binding into the each-block. Otherwise, the # assignments inside the each-block are not propagated out to ERB. b = binding params.keys.each do |var| eval "#{var.to_s} = \"#{params[var].to_s}\"", b end stylesheet = ERB.new(stylesheet).result(b) end stylesheet end |