Class: Pith::RenderContext
- Inherits:
-
Object
- Object
- Pith::RenderContext
- Includes:
- Tilt::CompileSite
- Defined in:
- lib/pith/render_context.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
- #content_for ⇒ Object
- #current_input ⇒ Object
- #href(target_ref) ⇒ Object
- #include(template_ref, locals = {}, &block) ⇒ Object (also: #inside)
-
#initialize(output) ⇒ RenderContext
constructor
A new instance of RenderContext.
- #link(target_ref, label = nil, attrs = {}) ⇒ Object
- #relative_url_to(target_path) ⇒ Object
- #render(input, locals = {}, &block) ⇒ Object
Constructor Details
#initialize(output) ⇒ RenderContext
Returns a new instance of RenderContext.
13 14 15 16 17 18 19 |
# File 'lib/pith/render_context.rb', line 13 def initialize(output) @output = output @page = @output.input @project = @page.project @input_stack = [] self.extend(project.config.helper_module) end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
21 22 23 |
# File 'lib/pith/render_context.rb', line 21 def output @output end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
22 23 24 |
# File 'lib/pith/render_context.rb', line 22 def page @page end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
23 24 25 |
# File 'lib/pith/render_context.rb', line 23 def project @project end |
Instance Method Details
#content_for ⇒ Object
52 53 54 |
# File 'lib/pith/render_context.rb', line 52 def content_for @content_for_hash ||= Hash.new { "" } end |
#current_input ⇒ Object
29 30 31 |
# File 'lib/pith/render_context.rb', line 29 def current_input @input_stack.last end |
#href(target_ref) ⇒ Object
64 65 66 |
# File 'lib/pith/render_context.rb', line 64 def href(target_ref) relative_url_to(resolve_reference(target_ref)) end |
#include(template_ref, locals = {}, &block) ⇒ Object Also known as: inside
42 43 44 45 46 47 48 |
# File 'lib/pith/render_context.rb', line 42 def include(template_ref, locals = {}, &block) content_block = if block_given? content = capture_haml(&block) proc { content } end render_ref(template_ref, locals, &content_block) end |
#link(target_ref, label = nil, attrs = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pith/render_context.rb', line 68 def link(target_ref, label = nil, attrs={}) if absolute_url?(target_ref) attrs['href'] = target_ref else target_path = resolve_reference(target_ref) attrs['href'] = relative_url_to(target_path) label ||= begin target_input = input(target_path) output.record_dependency_on(target_input) target_input.title rescue ReferenceError "???" end end # Loop through attrs hash, flatten the key, value # pairs for appending to the dom element/link attrs_flatten = attrs.each_pair.collect do |key, value| %Q{#{key}="#{value}"} end.join(' ') "<a #{attrs_flatten}>#{label}</a>" end |
#relative_url_to(target_path) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/pith/render_context.rb', line 56 def relative_url_to(target_path) url = target_path.relative_path_from(page.path.parent).to_s url = url.sub(/index\.html$/, "") if project.config.assume_directory_index url = url.sub(/\.html$/, "") if project.config.assume_content_negotiation url = "./" if url.empty? Pathname(url) end |
#render(input, locals = {}, &block) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/pith/render_context.rb', line 33 def render(input, locals = {}, &block) with_input(input) do result = input.render(self, locals, &block) layout_ref = input.["layout"] result = render_ref(layout_ref) { result } if layout_ref result end end |