module Merb module ViewContextMixin def link_to(name, url='', opts={}) %{#{name}} end # escape text for javascript. def escape_js(javascript) (javascript || '').gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } end # creates an tag with with an onclick containing # a js function # link_to_function('click me', "alert('hi!')") def link_to_function(name, function) %{#{name}} end def image_tag(img, path='/images/', opts={}) %{} end # calls .to_json on data. This will use fjson if installed # so it can be faster than escape_js def js(data) if data.respond_to? :to_json data.to_json else data.inspect.to_json end end # Helper method to cache a fragment. #

Article list

# # <% cache(:article_list) do %> # # <% end %> def cache(name, &block) return block.call unless caching_enabled? buffer = eval("_buf", block.binding) if fragment = ::Merb::Caching::Fragment.get(name) buffer.concat(fragment) else pos = buffer.length block.call ::Merb::Caching::Fragment.put(name, buffer[pos..-1]) end end # Requiring javascripts and stylesheets: # you can use require_js(:prototype) or require_css(:shinystyles) # from any view or layout and the scripts will only be included once # in the head of the final page. In the head of your layout you will # need to add these two tags: # # <%= include_required_js %> # <%= include_required_css %> # # --app/views/layouts/application.rhtml # # # # <%= include_required_js %> # <%= include_required_css %> # # # <%= catch_content :layout %> # # # # --app/views/whatever/index.rhtml # # <%= partial(:part1) %> # <%= partial(:part2) %> # # --app/views/whatever/_part1.rhtml # # <% require_js 'this' -%> # <% require_css 'that', 'another_one' -%> # # --app/views/whatever/_part2.rhtml # # <% require_js 'this', 'something_else' -%> # <% require_css 'that' -%> # require_js(:myjs) can be used to require any javascript # file anywhere in your templates. It will only include the # javascript tag once in the header def require_js(*js) @required_js ||= [] @required_js |= js end # require_css(:mystyles) can be used to require any javascript # file anywhere in your templates. It will only include the # javascript tag once in the header def require_css(*css) @required_css ||= [] @required_css |= css end # this goes in the head of your layout if you will be using # require_js def include_required_js js_include_tag(*@required_js) end # this goes in the head of your layout if you will be using # require_css def include_required_css css_link_tag(*@required_js) end # js_include_tag(:foo, :bar, :baz) will create a javascript # include tag for each script in the arguments. It will append # '.js' if it is left out of the call. def js_include_tag(*scripts) return nil if scripts.empty? scripts.inject('') do |memo,script| script = script.to_s memo << %Q|\n| end end # css_include_tag(:foo, :bar, :baz) will create a stylesheet # link tag for each stylesheet in the arguments. It will append # '.css' if it is left out of the call. def css_include_tag(*scripts) return nil if scripts.empty? scripts.inject('') do |memo,script| script = script.to_s memo << %Q|\n| end end end end