Module: TemplateInheritance::TemplateHelpers
- Defined in:
- lib/template-inheritance/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
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 47 48 |
# File 'lib/template-inheritance/helpers.rb', line 7 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
50 51 52 |
# File 'lib/template-inheritance/helpers.rb', line 50 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
59 60 61 62 63 64 |
# File 'lib/template-inheritance/helpers.rb', line 59 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.
88 89 90 91 |
# File 'lib/template-inheritance/helpers.rb', line 88 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
76 77 78 79 80 81 82 |
# File 'lib/template-inheritance/helpers.rb', line 76 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)
69 70 71 72 73 74 |
# File 'lib/template-inheritance/helpers.rb', line 69 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”
128 129 130 131 |
# File 'lib/template-inheritance/helpers.rb', line 128 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
121 122 123 124 |
# File 'lib/template-inheritance/helpers.rb', line 121 def includes(template, context = Hash.new) render normalize_template_path(template), context return true end |
#normalize_template_path(template) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/template-inheritance/helpers.rb', line 134 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”
113 114 115 116 117 118 |
# File 'lib/template-inheritance/helpers.rb', line 113 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.
100 101 102 103 104 105 106 107 108 |
# File 'lib/template-inheritance/helpers.rb', line 100 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) rescue Exceptions::TemplateNotFound # FIXME: this doesn't work raise SubtemplateNotFound, "Template #{path} doesn't exist in #{full_path}" end |