Class: Jekyll::UJPowertools::UnlessIsTruthyTag

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/tags/ifistruthy.rb

Constant Summary collapse

Syntax =
/(\w+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ UnlessIsTruthyTag

Returns a new instance of UnlessIsTruthyTag.



83
84
85
86
87
88
89
90
91
# File 'lib/tags/ifistruthy.rb', line 83

def initialize(tag_name, markup, tokens)
  super
  
  if markup =~ Syntax
    @variable_name = $1
  else
    raise SyntaxError, "Invalid syntax for unlessistruthy tag. Usage: {% unlessistruthy variable_name %}"
  end
end

Instance Method Details

#render(context) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tags/ifistruthy.rb', line 93

def render(context)
  variable = context[@variable_name]
  is_truthy = check_truthy(variable)
  
  # Split content at else tag
  else_index = nil
  @nodelist.each_with_index do |node, index|
    if node.respond_to?(:tag_name) && node.tag_name == 'else'
      else_index = index
      break
    end
  end
  
  if !is_truthy
    if else_index
      render_nodelist(@nodelist[0...else_index], context)
    else
      super(context)
    end
  else
    if else_index
      render_nodelist(@nodelist[(else_index + 1)..-1], context)
    else
      ''
    end
  end
end

#unknown_tag(tag_name, markup, tokens) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/tags/ifistruthy.rb', line 121

def unknown_tag(tag_name, markup, tokens)
  if tag_name == 'else'
    @nodelist << Liquid::ElseTag.new(tag_name, markup, tokens)
  else
    super
  end
end