Module: Roda::RodaPlugins::RodaTags::InstanceMethods

Defined in:
lib/roda/plugins/tags.rb

Instance Method Summary collapse

Instance Method Details

#block_is_template?(block) ⇒ Boolean

Returns true if the block is from an ERB or HAML template; false otherwise. Used to determine if html should be returned or concatenated to a view.

Examples

block_is_template?(block)

Returns:

  • (Boolean)


289
290
291
292
# File 'lib/roda/plugins/tags.rb', line 289

def block_is_template?(block) 
   block && (erb_block?(block) || 
     (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
end

#capture(block = '') ⇒ Object

:nodoc:



240
241
242
243
244
245
246
247
# File 'lib/roda/plugins/tags.rb', line 240

def capture(block = '') # :nodoc:
  buf_was = @output
  @output = block.is_a?(Proc) ? (eval('@_out_buf', block.binding) || @output) : block
  yield
  ret = @output
  @output = buf_was
  ret
end

#capture_html(*args, &block) ⇒ Object

Captures the html from a block of template code for erb or haml

Examples

capture_html(&block) => "...html..."


255
256
257
258
259
260
261
262
263
264
# File 'lib/roda/plugins/tags.rb', line 255

def capture_html(*args, &block) 
  if self.respond_to?(:is_haml?) && is_haml?
    block_is_haml?(block) ? capture_haml(*args, &block) : block.call
  elsif erb_buffer?
    result_text = capture_block(*args, &block)
    result_text.present? ? result_text : (block_given? && block.call(*args))
  else # theres no template to capture, invoke the block directly
    block.call(*args)
  end
end

#concat_content(text = '') ⇒ Object

Outputs the given text to the templates buffer directly.

Examples

concat_content("This will be output to the template buffer in erb or haml")


272
273
274
275
276
277
278
279
280
# File 'lib/roda/plugins/tags.rb', line 272

def concat_content(text = '') 
  if self.respond_to?(:is_haml?) && is_haml?
    haml_concat(text)
  elsif :erb_buffer?
    buffer_concat(text)
  else # theres no template to concat, return the text directly
    text
  end
end

#merge_attr_classes(attr, *classes) ⇒ Object

Update the :class entry in the attr hash with the given classes and returns attr.

attr = { class: 'alert', id: :idval }

merge_attr_classes(attr, 'alert-info')  #=> { class: 'alert alert-info', id: :idval }

merge_attr_classes(attr, [:alert, 'alert-info']) 
  #=> { class: 'alert alert-info', id: :idval }


205
206
207
208
209
210
# File 'lib/roda/plugins/tags.rb', line 205

def merge_attr_classes(attr, *classes)
  attr[:class] = [] if attr[:class].blank?
  attr[:class] = merge_classes(attr[:class], *classes)
  attr[:class] = nil if attr[:class] == '' # set to nil to remove from tag output
  attr
end

#merge_classes(*classes) ⇒ Object

Return an alphabetized string that includes all given class values.

Handles a combination of arrays, strings & symbols being passed in.

attr = { class: 'alert', id: :idval }

merge_classes(attr[:class], ['alert', 'alert-info'])  #=> 'alert alert-info'

merge_classes(attr[:class], :text)  #=> 'alert text'

merge_classes(attr[:class], [:text, :'alert-info'])  #=> 'alert alert-info text'


225
226
227
228
229
230
231
232
233
# File 'lib/roda/plugins/tags.rb', line 225

def merge_classes(*classes)
  klasses = []
  classes.each do |c|
    klasses << c.to_s if c.is_a?(Symbol)
    c.split(/\s+/).each { |x| klasses << x.to_s  } if c.is_a?(String)
    c.each { |i| klasses << i.to_s } if c.is_a?(Array)
  end
  klasses.compact.uniq.sort.join(' ').strip
end

#output_is_xhtml?Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/roda/plugins/tags.rb', line 295

def output_is_xhtml?
  opts[:tags][:tag_output_format_is_xhtml]
end

#tag(*args, &block) ⇒ Object

Returns markup for tag name.

Optionally contents may be passed, which is literal content for spanning tags such as textarea, etc.

A hash of attrs may be passed as the second or third argument.

Self closing tags such as <br/>, <input/>, etc are automatically closed depending on output format, HTML vs XHTML.

Boolean attributes like “selected”, “checked” etc, are mirrored or removed when true or false.

Examples

Self closing tags:

tag(:br)
# => <br> or <br/> if XHTML

tag(:hr, class: "space")
# => <hr class="space">

Multi line tags:

tag(:div)
# => <div></div>

tag(:div, 'content')
# => <div>content</div>

tag(:div, 'content', id: 'comment')
# => <div id="comment">content</div>

tag(:div, id: 'comment')  # NB! no content
# => <div id="comment"></div>

Single line tags:

tag(:h1,'Header')
# => <h1>Header</h1>

tag(:abbr, 'WHO', :title => "World Health Organization")
# => <abbr title="World Health Organization">WHO</abbr>

Working with blocks

tag(:div) do
  tag(:p, 'Hello World')
end
# => <div><p>Hello World</p></div>

<% tag(:div) do %>
  <p>Paragraph 1</p>
  <%= tag(:p, 'Paragraph 2') %>
  <p>Paragraph 3</p>
<% end %>
# => 
  <div>
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>
    <p>Paragraph 3</p>
  </div>

# NB! ignored tag contents if given a block
<% tag(:div, 'ignored tag-content') do  %>
  <%= tag(:label, 'Comments:', for: :comments)  %>
  <%= tag(:textarea,'textarea contents', id: :comments) %>
<% end  %>
# => 
  <div>
    <label for="comments">Comments:</label>
    <textarea id="comments">
      textarea contents
    </textarea>
  </div>

Boolean attributes:

tag(:input, type: :checkbox, checked: true)
# => <input type="checkbox" checked="checked">

tag(:option, 'Sinatra', value: "1", selected: true)
# => <option value="1" selected>Sinatra</option>

tag(:option, 'PHP', value: "0", selected: false)
# => <option value="0">PHP</option>


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/roda/plugins/tags.rb', line 180

def tag(*args, &block)
  name = args.first
  attrs = args.last.is_a?(Hash) ? args.pop : {}
  newline = attrs[:newline] # save before it gets tainted

  tag_content = block_given? ? capture_html(&block) : args[1]  # content

  if self_closing_tag?(name)
    tag_html = self_closing_tag(name, attrs)
  else
    tag_html = "#{open_tag(name, attrs)}#{tag_contents_for(name, tag_content, newline)}" 
    tag_html << closing_tag(name)
  end
  block_is_template?(block) ? concat_content(tag_html) : tag_html
end