Module: Rango::TemplateHelpers
- Defined in:
- lib/rango/templates/helpers.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#block(name, value = nil, &block) ⇒ Object
post/show.html: it’s block is the block we like to see in output post/base.html base.html: here it will be rendered, so we need block to returns the correct block code.
-
#clear_block(name) ⇒ Object
Clears default content of given block.
- #enhance_block(name, value = nil, &block) ⇒ Object
-
#extend_block(name, value = nil, &block) ⇒ Object
-
extend_block(:head) do = pupu :lighter, syntax: “html”, theme: “standard” = block(:head).
-
-
#extends(path) ⇒ Object
extends “base.html”.
- #includes(template, context = Hash.new) ⇒ Object
- #normalize_template_path(template) ⇒ Object
-
#partial(template, extra_context = Hash.new) ⇒ Object
partial “products/list”.
-
#render(path, context = Hash.new) ⇒ Object
Low-level rendering method for templates.
Class Method Details
.extended(scope) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rango/templates/helpers.rb', line 10 def self.extended(scope) class << scope attr_accessor :template attr_accessor :context # @example Capture being used in a .html.erb page: # <% @foo = capture do %> # <p>Some Foo content!</p> # <% end %> # # @params [*args] Arguments to pass to the block. # @params [&block] The template block to call. # @return [String] The output of the block. # @api private def capture(*args, &block) capture_method = "capture_#{self.template.adapter}" if self.respond_to?(capture_method) # tilt doesn't support @_out_buf for haml self.send("capture_#{self.template.adapter}", *args, &block) else # @_out_buf comes from tilt unless self.instance_variable_defined?("@_out_buf") raise "Adapter #{self.template.adapter} doesn't support capturing" end _old_buf, @_out_buf = @_out_buf, "" block.call(*args) @_out_buf = _old_buf.chomp.strip end end def concat(string) concat_method = "concat_#{self.template.adapter}" if self.respond_to?(concat_method) # tilt doesn't support @_out_buf for haml self.send("concat_#{self.template.adapter}", string) else # @_out_buf comes from tilt unless self.instance_variable_defined?("@_out_buf") raise "Adapter #{self.template.adapter} doesn't support concating" end @_out_buf << string end end end end |
.included(scope_class) ⇒ Object
53 54 55 |
# File 'lib/rango/templates/helpers.rb', line 53 def self.included(scope_class) scope_class.class_eval { attr_accessor :template } end |
Instance Method Details
#block(name, value = nil, &block) ⇒ Object
post/show.html: it’s block is the block we like to see in output post/base.html base.html: here it will be rendered, so we need block to returns the correct block code
62 63 64 65 66 67 |
# File 'lib/rango/templates/helpers.rb', line 62 def block(name, value = nil, &block) raise ArgumentError, "Block has to have a name!" if name.nil? raise ArgumentError, "You have to provide value or block, not both of them!" if value && block self.template.blocks[name] ||= block ? self.template.scope.capture(&block) : value return self.template.blocks[name] end |
#clear_block(name) ⇒ Object
Clears default content of given block.
91 92 93 94 |
# File 'lib/rango/templates/helpers.rb', line 91 def clear_block(name) raise ArgumentError, "You need to specify name of block to clear." if name.nil? self.template.blocks[name] = String.new end |
#enhance_block(name, value = nil, &block) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/rango/templates/helpers.rb', line 79 def enhance_block(name, value = nil, &block) raise ArgumentError, "Block has to have a name!" if name.nil? raise ArgumentError, "You have to provide value or block, not both of them!" if value && block value = self.template.scope.capture(&block) if value.nil? && block self.template.blocks[name] = value if value return self.template.blocks[name] end |
#extend_block(name, value = nil, &block) ⇒ Object
-
extend_block(:head) do
pupu :lighter, syntax: “html”, theme: “standard”
block(:head)
72 73 74 75 76 77 |
# File 'lib/rango/templates/helpers.rb', line 72 def extend_block(name, value = nil, &block) unless self.template.blocks[name] raise NameError, "Block #{name.inspect} wasn't defined yet, you can't extend it!" end self.enhance_block(name, value, &block) end |
#extends(path) ⇒ Object
extends “base.html”
131 132 133 134 |
# File 'lib/rango/templates/helpers.rb', line 131 def extends(path) # we can't just create a new template, because it has to do it after it reads the whole file self.template.supertemplate = normalize_template_path(path) end |
#includes(template, context = Hash.new) ⇒ Object
124 125 126 127 |
# File 'lib/rango/templates/helpers.rb', line 124 def includes(template, context = Hash.new) render normalize_template_path(template), context return true end |
#normalize_template_path(template) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/rango/templates/helpers.rb', line 137 def normalize_template_path(template) if template.start_with?("./") File.(File.join(File.dirname(self.template.fullpath), template)) elsif template.start_with?("../") File.(File.join(File.dirname(self.template.fullpath), "..", template)) else template end end |
#partial(template, extra_context = Hash.new) ⇒ Object
partial “products/list”
116 117 118 119 120 121 |
# File 'lib/rango/templates/helpers.rb', line 116 def partial(template, extra_context = Hash.new) # NOTE: we can't use File.split because it normalize the path, # so "./base.html" will be the same as "base.html", but it shouldn't be *path, basename = template.split("/") render File.join(path.join("/"), "_#{basename}"), self.template.context.merge(extra_context) end |
#render(path, context = Hash.new) ⇒ Object
Low-level rendering method for templates.
103 104 105 106 107 108 109 110 111 |
# File 'lib/rango/templates/helpers.rb', line 103 def render(path, context = Hash.new) full_path = normalize_template_path(path) original_template = self.template template = Rango::Template.new(full_path, self) # self is scope self.template = original_template return template.render(context) rescue Exceptions::TemplateNotFound # FIXME: this doesn't work raise SubtemplateNotFound, "Template #{path} doesn't exist in #{full_path}" end |