Module: TemplateInheritance::TemplateHelpers
- Defined in:
- lib/template-inheritance/helpers.rb,
lib/template-inheritance/adapters/padrino.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_normalize_template_path ⇒ Object
-
#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(path) ⇒ 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
5 6 7 8 9 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 |
# File 'lib/template-inheritance/helpers.rb', line 5 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
48 49 50 |
# File 'lib/template-inheritance/helpers.rb', line 48 def self.included(scope_class) scope_class.class_eval { attr_accessor :template } end |
Instance Method Details
#_normalize_template_path ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/template-inheritance/adapters/padrino.rb', line 20 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 |
#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
57 58 59 60 61 62 |
# File 'lib/template-inheritance/helpers.rb', line 57 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.
86 87 88 89 |
# File 'lib/template-inheritance/helpers.rb', line 86 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
74 75 76 77 78 79 80 |
# File 'lib/template-inheritance/helpers.rb', line 74 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)
67 68 69 70 71 72 |
# File 'lib/template-inheritance/helpers.rb', line 67 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”
124 125 126 127 |
# File 'lib/template-inheritance/helpers.rb', line 124 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
117 118 119 120 |
# File 'lib/template-inheritance/helpers.rb', line 117 def includes(template, context = Hash.new) render normalize_template_path(template), context return true end |
#normalize_template_path(path) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/template-inheritance/helpers.rb', line 130 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”
109 110 111 112 113 114 |
# File 'lib/template-inheritance/helpers.rb', line 109 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.
98 99 100 101 102 103 104 |
# File 'lib/template-inheritance/helpers.rb', line 98 def render(path, context = Hash.new) full_path = normalize_template_path(path) original_template = self.template template = TemplateInheritance::Template.new(full_path, self) # self is scope self.template = original_template return template.render(context) end |