Module: Liquid::StandardFilters

Defined in:
lib/liquid/standardfilters.rb

Instance Method Summary collapse

Instance Method Details

#append(input, string) ⇒ Object

add one string to another



127
128
129
# File 'lib/liquid/standardfilters.rb', line 127

def append(input, string)
  input.to_s + string.to_s
end

#capitalize(input) ⇒ Object

capitalize words in the input centence



23
24
25
# File 'lib/liquid/standardfilters.rb', line 23

def capitalize(input)
  input.to_s.capitalize
end

#date(input, format) ⇒ Object

Reformat a date

%a - The abbreviated weekday name (``Sun'')
%A - The  full  weekday  name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The  full  month  name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM''  or  ``PM'')
%S - Second of the minute (00..60)
%U - Week  number  of the current year,
        starting with the first Sunday as the first
        day of the first week (00..53)
%W - Week  number  of the current year,
        starting with the first Monday as the first
        day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/liquid/standardfilters.rb', line 169

def date(input, format)

  if format.to_s.empty?
    return input.to_s
  end

  if ((input.is_a?(String) && !/^\d+$/.match(input.to_s).nil?) || input.is_a?(Integer)) && input.to_i > 0
    input = Time.at(input.to_i)
  end

  date = input.is_a?(String) ? Time.parse(input) : input

  if date.respond_to?(:strftime)
    date.strftime(format.to_s)
  else
    input
  end
rescue => e
  input
end

#divided_by(input, operand) ⇒ Object

division



224
225
226
# File 'lib/liquid/standardfilters.rb', line 224

def divided_by(input, operand)
  to_number(input) / to_number(operand)
end

#downcase(input) ⇒ Object

convert a input string to DOWNCASE



13
14
15
# File 'lib/liquid/standardfilters.rb', line 13

def downcase(input)
  input.to_s.downcase
end

#escape(input) ⇒ Object Also known as: h



27
28
29
# File 'lib/liquid/standardfilters.rb', line 27

def escape(input)
  CGI.escapeHTML(input) rescue input
end

#escape_once(input) ⇒ Object



31
32
33
34
35
# File 'lib/liquid/standardfilters.rb', line 31

def escape_once(input)
  ActionView::Helpers::TagHelper.escape_once(input)
rescue NameError
  input
end

#first(array) ⇒ Object

Get the first element of the passed in array

Example:

{{ product.images | first | to_img }}


195
196
197
# File 'lib/liquid/standardfilters.rb', line 195

def first(array)
  array.first if array.respond_to?(:first)
end

#join(input, array_glue = ' ', hash_glue = nil) ⇒ Object

Join elements of the array with certain character between them



74
75
76
77
78
79
80
81
# File 'lib/liquid/standardfilters.rb', line 74

def join(input, array_glue = ' ', hash_glue = nil)
  hash_glue ||= array_glue

  # translate from hash to array if needed
  input = input.map{|k,v| "#{k}#{hash_glue}#{v}" } if input.is_a?(Hash)

  [input].flatten.join(array_glue)
end

#last(array) ⇒ Object

Get the last element of the passed in array

Example:

{{ product.images | last | to_img }}


204
205
206
# File 'lib/liquid/standardfilters.rb', line 204

def last(array)
  array.last if array.respond_to?(:last)
end

#map(input, property) ⇒ Object

map/collect on a given property



97
98
99
100
101
102
103
104
# File 'lib/liquid/standardfilters.rb', line 97

def map(input, property)
  ary = [input].flatten
  if ary.first.respond_to?('[]') and !ary.first[property].nil?
    ary.map {|e| e[property] }
  elsif ary.first.respond_to?(property)
    ary.map {|e| e.send(property) }
  end
end

#minus(input, operand) ⇒ Object

subtraction



214
215
216
# File 'lib/liquid/standardfilters.rb', line 214

def minus(input, operand)
  to_number(input) - to_number(operand)
end

#modulo(input, operand) ⇒ Object



228
229
230
# File 'lib/liquid/standardfilters.rb', line 228

def modulo(input, operand)
  to_number(input) % to_number(operand)
end

#newline_to_br(input) ⇒ Object

Add <br /> tags in front of all newlines in input string



137
138
139
# File 'lib/liquid/standardfilters.rb', line 137

def newline_to_br(input)
  input.to_s.gsub(/\n/, "<br />\n")
end

#plus(input, operand) ⇒ Object

addition



209
210
211
# File 'lib/liquid/standardfilters.rb', line 209

def plus(input, operand)
  to_number(input) + to_number(operand)
end

#prepend(input, string) ⇒ Object

prepend a string to another



132
133
134
# File 'lib/liquid/standardfilters.rb', line 132

def prepend(input, string)
  string.to_s + input.to_s
end

#remove(input, string) ⇒ Object

remove a substring



117
118
119
# File 'lib/liquid/standardfilters.rb', line 117

def remove(input, string)
  input.to_s.gsub(string, '')
end

#remove_first(input, string) ⇒ Object

remove the first occurrences of a substring



122
123
124
# File 'lib/liquid/standardfilters.rb', line 122

def remove_first(input, string)
  input.to_s.sub(string, '')
end

#replace(input, string, replacement = '') ⇒ Object

Replace occurrences of a string with another



107
108
109
# File 'lib/liquid/standardfilters.rb', line 107

def replace(input, string, replacement = '')
  input.to_s.gsub(string, replacement)
end

#replace_first(input, string, replacement = '') ⇒ Object

Replace the first occurrences of a string with another



112
113
114
# File 'lib/liquid/standardfilters.rb', line 112

def replace_first(input, string, replacement = '')
  input.to_s.sub(string, replacement)
end

#size(input) ⇒ Object

Return the size of an array or of an string



8
9
10
# File 'lib/liquid/standardfilters.rb', line 8

def size(input)
  input.respond_to?(:size) ? input.size : 0
end

#sort(input, property = nil) ⇒ Object

Sort elements of the array provide optional property with which to sort an array of hashes or drops



85
86
87
88
89
90
91
92
93
94
# File 'lib/liquid/standardfilters.rb', line 85

def sort(input, property = nil)
  ary = [input].flatten
  if property.nil?
    ary.sort
  elsif ary.first.respond_to?('[]') and !ary.first[property].nil?
    ary.sort {|a,b| a[property] <=> b[property] }
  elsif ary.first.respond_to?(property)
    ary.sort {|a,b| a.send(property) <=> b.send(property) }
  end
end

#split(input, pattern) ⇒ Object

Split input string into an array of substrings separated by given pattern.

Example:

<div class="summary">{{ post | split '//' | first }}</div>


60
61
62
# File 'lib/liquid/standardfilters.rb', line 60

def split(input, pattern)
  input.split(pattern)
end

#strip_html(input) ⇒ Object



64
65
66
# File 'lib/liquid/standardfilters.rb', line 64

def strip_html(input)
  input.to_s.gsub(/<script.*?<\/script>/, '').gsub(/<!--.*?-->/, '').gsub(/<.*?>/, '')
end

#strip_newlines(input) ⇒ Object

Remove all newlines from the string



69
70
71
# File 'lib/liquid/standardfilters.rb', line 69

def strip_newlines(input)
  input.to_s.gsub(/\n/, '')
end

#times(input, operand) ⇒ Object

multiplication



219
220
221
# File 'lib/liquid/standardfilters.rb', line 219

def times(input, operand)
  to_number(input) * to_number(operand)
end

#truncate(input, length = 50, truncate_string = "...") ⇒ Object

Truncate a string down to x characters



40
41
42
43
44
45
# File 'lib/liquid/standardfilters.rb', line 40

def truncate(input, length = 50, truncate_string = "...")
  if input.nil? then return end
  l = length.to_i - truncate_string.length
  l = 0 if l < 0
  input.length > length.to_i ? input[0...l] + truncate_string : input
end

#truncatewords(input, words = 15, truncate_string = "...") ⇒ Object



47
48
49
50
51
52
53
# File 'lib/liquid/standardfilters.rb', line 47

def truncatewords(input, words = 15, truncate_string = "...")
  if input.nil? then return end
  wordlist = input.to_s.split
  l = words.to_i - 1
  l = 0 if l < 0
  wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input
end

#upcase(input) ⇒ Object

convert a input string to UPCASE



18
19
20
# File 'lib/liquid/standardfilters.rb', line 18

def upcase(input)
  input.to_s.upcase
end