Class: NicePartials::Partial
- Inherits:
-
Object
- Object
- NicePartials::Partial
- Defined in:
- lib/nice_partials/partial.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#output_buffer ⇒ Object
<%= render “nice_partial” do |p| %> <% p.content_for :title, “Yo” %> This content can be accessed through calling ‘yield`.
Instance Method Summary collapse
- #capture(*arguments, &block) ⇒ Object
-
#content_for(name, content = nil, *arguments, &block) ⇒ Object
Similar to Rails’ built-in ‘content_for` except it defers any block execution and lets you pass arguments into it, like so:.
- #content_for?(name) ⇒ Boolean
- #helpers(&block) ⇒ Object
-
#initialize(view_context) ⇒ Partial
constructor
A new instance of Partial.
- #yield(*arguments, &block) ⇒ Object
Constructor Details
#initialize(view_context) ⇒ Partial
Returns a new instance of Partial.
18 19 20 21 |
# File 'lib/nice_partials/partial.rb', line 18 def initialize(view_context) @view_context = view_context @contents = Hash.new { |h, k| h[k] = Section.new(@view_context) } end |
Instance Attribute Details
#output_buffer ⇒ Object
<%= render “nice_partial” do |p| %>
<% p.content_for :title, "Yo" %>
This content can be accessed through calling `yield`.
<% end %>
Then in the nice_partial:
<%= content.content_for :title %> # => "Yo"
<%= content.output_buffer %> # => "This line is printed to the `output_buffer`."
16 17 18 |
# File 'lib/nice_partials/partial.rb', line 16 def output_buffer @output_buffer end |
Instance Method Details
#capture(*arguments, &block) ⇒ Object
53 54 55 |
# File 'lib/nice_partials/partial.rb', line 53 def capture(*arguments, &block) self.output_buffer = @view_context.capture(*arguments, self, &block) end |
#content_for(name, content = nil, *arguments, &block) ⇒ Object
Similar to Rails’ built-in ‘content_for` except it defers any block execution and lets you pass arguments into it, like so:
# Here we store a block with some eventual content…
<% partial.content_for :title do |tag|
<%= tag.h1 %>
<% end %>
# …which is then invoked with some predefined options later.
<%= partial.content_for :title, tag.(class: "text-bold") %>
45 46 47 |
# File 'lib/nice_partials/partial.rb', line 45 def content_for(name, content = nil, *arguments, &block) @contents[name].content_for(content, *arguments, &block) end |
#content_for?(name) ⇒ Boolean
49 50 51 |
# File 'lib/nice_partials/partial.rb', line 49 def content_for?(name) @contents[name].content? end |
#helpers(&block) ⇒ Object
31 32 33 |
# File 'lib/nice_partials/partial.rb', line 31 def helpers(&block) class_eval &block end |
#yield(*arguments, &block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/nice_partials/partial.rb', line 23 def yield(*arguments, &block) if arguments.empty? output_buffer else content_for(*arguments, &block) end end |