| 15 | | |
| 16 | | # Markaby helpers for Rails. |
| 17 | | module ActionControllerHelpers |
| 18 | | # Returns a string of HTML built from the attached +block+. Any +options+ are |
| 19 | | # passed into the render method. |
| 20 | | # |
| 21 | | # Use this method in your controllers to output Markaby directly from inside. |
| 22 | | def render_markaby(options = {}, &block) |
| 23 | | render options.merge({ :text => Builder.new(options[:locals], self, &block).to_s }) |
| 24 | | end |
| 25 | | end |
| 26 | | |
| 27 | | class ActionViewTemplateHandler # :nodoc: |
| 28 | | def initialize(action_view) |
| 29 | | @action_view = action_view |
| 30 | | end |
| 31 | | def render(template, local_assigns, file_path) |
| 32 | | template = Template.new(template) |
| 33 | | template.path = file_path |
| 34 | | template.render(@action_view.assigns.merge(local_assigns), @action_view) |
| 35 | | end |
| 36 | | end |
| 37 | | |
| 38 | | class FauxErbout < ::Builder::BlankSlate # :nodoc: |
| 39 | | def initialize(builder) |
| 40 | | @builder = builder |
| 41 | | end |
| 42 | | def nil? # see ActionView::Helpers::CaptureHelper#capture |
| 43 | | true |
| 44 | | end |
| 45 | | def method_missing(*args, &block) |
| 46 | | @builder.send *args, &block |
| 47 | | end |
| 48 | | end |
| 49 | | |
| 50 | | class Builder # :nodoc: |
| 51 | | def flash(*args) |
| 52 | | @helpers.controller.send(:flash, *args) |
| 53 | | end |
| 54 | | |
| 55 | | # Emulate ERB to satisfy helpers like <tt>form_for</tt>. |
| 56 | | def _erbout |
| 57 | | @_erbout ||= FauxErbout.new(self) |
| | 15 | module Rails |
| | 16 | # Markaby helpers for Rails. |
| | 17 | module ActionControllerHelpers |
| | 18 | # Returns a string of HTML built from the attached +block+. Any +options+ are |
| | 19 | # passed into the render method. |
| | 20 | # |
| | 21 | # Use this method in your controllers to output Markaby directly from inside. |
| | 22 | def render_markaby(options = {}, &block) |
| | 23 | render options.merge({ :text => Builder.new(options[:locals], self, &block).to_s }) |
| | 24 | end |
| 60 | | # Content_for will store the given block in an instance variable for later use |
| 61 | | # in another template or in the layout. |
| 62 | | # |
| 63 | | # The name of the instance variable is content_for_<name> to stay consistent |
| 64 | | # with @content_for_layout which is used by ActionView's layouts. |
| 65 | | # |
| 66 | | # Example: |
| 67 | | # |
| 68 | | # content_for("header") do |
| 69 | | # h1 "Half Shark and Half Lion" |
| 70 | | # end |
| 71 | | # |
| 72 | | # If used several times, the variable will contain all the parts concatenated. |
| 73 | | def content_for(name, &block) |
| 74 | | @helpers.assigns["content_for_#{name}"] = |
| 75 | | eval("@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)") |
| | 27 | class ActionViewTemplateHandler # :nodoc: |
| | 28 | def initialize(action_view) |
| | 29 | @action_view = action_view |
| | 30 | end |
| | 31 | def render(template, local_assigns, file_path) |
| | 32 | template = Template.new(template) |
| | 33 | template.path = file_path |
| | 34 | template.render(@action_view.assigns.merge(local_assigns), @action_view) |
| | 35 | end |
| | 37 | |
| | 38 | class Builder < Markaby::Builder # :nodoc: |
| | 39 | def initialize(*args, &block) |
| | 40 | super *args, &block |
| | 41 | |
| | 42 | @assigns.each { |k, v| @helpers.instance_variable_set("@#{k}", v) } |
| | 43 | end |
| | 44 | |
| | 45 | def flash(*args) |
| | 46 | @helpers.controller.send(:flash, *args) |
| | 47 | end |
| | 48 | |
| | 49 | # Emulate ERB to satisfy helpers like <tt>form_for</tt>. |
| | 50 | def _erbout |
| | 51 | @_erbout ||= FauxErbout.new(self) |
| | 52 | end |
| | 53 | |
| | 54 | # Content_for will store the given block in an instance variable for later use |
| | 55 | # in another template or in the layout. |
| | 56 | # |
| | 57 | # The name of the instance variable is content_for_<name> to stay consistent |
| | 58 | # with @content_for_layout which is used by ActionView's layouts. |
| | 59 | # |
| | 60 | # Example: |
| | 61 | # |
| | 62 | # content_for("header") do |
| | 63 | # h1 "Half Shark and Half Lion" |
| | 64 | # end |
| | 65 | # |
| | 66 | # If used several times, the variable will contain all the parts concatenated. |
| | 67 | def content_for(name, &block) |
| | 68 | @helpers.assigns["content_for_#{name}"] = |
| | 69 | eval("@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)") |
| | 70 | end |
| | 71 | end |
| | 72 | |
| | 73 | |
| | 74 | |
| | 75 | Template.builder_class = Builder |
| | 76 | |
| | 77 | class FauxErbout < ::Builder::BlankSlate # :nodoc: |
| | 78 | def initialize(builder) |
| | 79 | @builder = builder |
| | 80 | end |
| | 81 | def nil? # see ActionView::Helpers::CaptureHelper#capture |
| | 82 | true |
| | 83 | end |
| | 84 | def method_missing(*args, &block) |
| | 85 | @builder.send *args, &block |
| | 86 | end |
| | 87 | end |
| | 88 | |