Class: Ruty::Tags::IfChanged

Inherits:
Ruty::Tag show all
Defined in:
lib/ruty/tags/looptools.rb

Overview

just render everything between the tag and the closing endifchanged tag if the given variable hasn’t changed from the last iteration. If no variable is given the block will check it’s own rendering against the rendering of the last iteration.

for day in days %

<div class="day">
  <h2>{{ day.name|escape }}</h2>
  {% for entry in day.entries %}
    {% ifchanged entry.pub_date.hour %}
      <h3>{{ entry.pub_date }}</h3>
    {% endifchanged %}
    ...
  {% endfor %}
</div>

endfor %

Instance Method Summary collapse

Constructor Details

#initialize(parser, argstring) ⇒ IfChanged

Returns a new instance of IfChanged.



56
57
58
59
60
61
62
63
# File 'lib/ruty/tags/looptools.rb', line 56

def initialize parser, argstring
  args = parser.parse_arguments(argstring)
  if not [0, 1].include?(args.length)
    parser.fail('ifchanged-tag takes at most one argument')
  end
  @arg = args[0]
  @body = parser.parse_until { |name, a| name == :endifchanged }
end

Instance Method Details

#render_node(context, stream) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruty/tags/looptools.rb', line 65

def render_node context, stream
  if not @arg
    substream = Datastructure::OutputStream.new
    @body.render_node(context, substream)
    this_iteration = substream.to_s
    if this_iteration != context[self]
      block << this_iteration
      context[self] = this_iteration
    end
  else
    item = (@arg.is_a?(Symbol)) ? context.resolve(@arg) : @arg
    if item != context[self]
      @body.render_node(context, stream)
      context[self] = item
    end
  end
  nil
end