module Liquid::StandardFilters

Public Instance Methods

append(input, string) click to toggle source

add one string to another

# File lib/liquid/standardfilters.rb, line 136
def append(input, string)
  input.to_s + string.to_s
end
capitalize(input) click to toggle source

capitalize words in the input centence

# File lib/liquid/standardfilters.rb, line 25
def capitalize(input)
  input.to_s.capitalize
end
date(input, format) click to toggle source

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
# File lib/liquid/standardfilters.rb, line 178
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 = if input.is_a?(String)
    case input.downcase
    when 'now', 'today'
      Time.now
    else
      Time.parse(input)
    end
  else
    input
  end

  if date.respond_to?(:strftime)
    date.strftime(format.to_s)
  else
    input
  end
rescue
  input
end
divided_by(input, operand) click to toggle source

division

# File lib/liquid/standardfilters.rb, line 242
def divided_by(input, operand)
  apply_operation(input, operand, :/)
end
downcase(input) click to toggle source

convert an input string to DOWNCASE

# File lib/liquid/standardfilters.rb, line 15
def downcase(input)
  input.to_s.downcase
end
escape(input) click to toggle source
# File lib/liquid/standardfilters.rb, line 29
def escape(input)
  CGI.escapeHTML(input) rescue input
end
Also aliased as: h
escape_once(input) click to toggle source
# File lib/liquid/standardfilters.rb, line 33
def escape_once(input)
  ActionView::Helpers::TagHelper.escape_once(input)
rescue NameError
  input
end
first(array) click to toggle source

Get the first element of the passed in array

Example:

{{ product.images | first | to_img }}
# File lib/liquid/standardfilters.rb, line 213
def first(array)
  array.first if array.respond_to?(:first)
end
h(input)
Alias for: escape
join(input, glue = ' ') click to toggle source

Join elements of the array with certain character between them

# File lib/liquid/standardfilters.rb, line 77
def join(input, glue = ' ')
  [input].flatten.join(glue)
end
last(array) click to toggle source

Get the last element of the passed in array

Example:

{{ product.images | last | to_img }}
# File lib/liquid/standardfilters.rb, line 222
def last(array)
  array.last if array.respond_to?(:last)
end
map(input, property) click to toggle source

map/collect on a given property

# File lib/liquid/standardfilters.rb, line 101
def map(input, property)
  ary = [input].flatten
  ary.map do |e|
    e = e.call if e.is_a?(Proc)
    e = e.to_liquid if e.respond_to?(:to_liquid)

    if property == "to_liquid"
      e
    elsif e.respond_to?(:[])
      e[property]
    end
  end
end
minus(input, operand) click to toggle source

subtraction

# File lib/liquid/standardfilters.rb, line 232
def minus(input, operand)
  apply_operation(input, operand, :-)
end
modulo(input, operand) click to toggle source
# File lib/liquid/standardfilters.rb, line 246
def modulo(input, operand)
  apply_operation(input, operand, :%)
end
newline_to_br(input) click to toggle source

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

# File lib/liquid/standardfilters.rb, line 146
def newline_to_br(input)
  input.to_s.gsub(/\n/, "<br />\n")
end
plus(input, operand) click to toggle source

addition

# File lib/liquid/standardfilters.rb, line 227
def plus(input, operand)
  apply_operation(input, operand, :+)
end
prepend(input, string) click to toggle source

prepend a string to another

# File lib/liquid/standardfilters.rb, line 141
def prepend(input, string)
  string.to_s + input.to_s
end
remove(input, string) click to toggle source

remove a substring

# File lib/liquid/standardfilters.rb, line 126
def remove(input, string)
  input.to_s.gsub(string, '')
end
remove_first(input, string) click to toggle source

remove the first occurrences of a substring

# File lib/liquid/standardfilters.rb, line 131
def remove_first(input, string)
  input.to_s.sub(string, '')
end
replace(input, string, replacement = '') click to toggle source

Replace occurrences of a string with another

# File lib/liquid/standardfilters.rb, line 116
def replace(input, string, replacement = '')
  input.to_s.gsub(string, replacement.to_s)
end
replace_first(input, string, replacement = '') click to toggle source

Replace the first occurrences of a string with another

# File lib/liquid/standardfilters.rb, line 121
def replace_first(input, string, replacement = '')
  input.to_s.sub(string, replacement.to_s)
end
reverse(input) click to toggle source

Reverse the elements of an array

# File lib/liquid/standardfilters.rb, line 95
def reverse(input)
  ary = [input].flatten
  ary.reverse
end
size(input) click to toggle source

Return the size of an array or of an string

# File lib/liquid/standardfilters.rb, line 9
def size(input)

  input.respond_to?(:size) ? input.size : 0
end
sort(input, property = nil) click to toggle source

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

# File lib/liquid/standardfilters.rb, line 83
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) click to toggle source

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

Example:

<div class="summary">{{ post | split '//' | first }}</div>
# File lib/liquid/standardfilters.rb, line 63
def split(input, pattern)
  input.split(pattern)
end
strip_html(input) click to toggle source
# File lib/liquid/standardfilters.rb, line 67
def strip_html(input)
  input.to_s.gsub(/<script.*?<\/script>/, '').gsub(/<!--.*?-->/, '').gsub(/<style.*?<\/style>/, '').gsub(/<.*?>/, '')
end
strip_newlines(input) click to toggle source

Remove all newlines from the string

# File lib/liquid/standardfilters.rb, line 72
def strip_newlines(input)
  input.to_s.gsub(/\r?\n/, '')
end
times(input, operand) click to toggle source

multiplication

# File lib/liquid/standardfilters.rb, line 237
def times(input, operand)
  apply_operation(input, operand, :*)
end
truncate(input, length = 50, truncate_string = "...") click to toggle source

Truncate a string down to x characters

# File lib/liquid/standardfilters.rb, line 42
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
  truncated = RUBY_VERSION[0,3] == "1.8" ? input.scan(/./u)[0...l].to_s : input[0...l]
  input.length > length.to_i ? truncated + truncate_string : input
end
truncatewords(input, words = 15, truncate_string = "...") click to toggle source
# File lib/liquid/standardfilters.rb, line 50
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) click to toggle source

convert an input string to UPCASE

# File lib/liquid/standardfilters.rb, line 20
def upcase(input)
  input.to_s.upcase
end

Private Instance Methods

apply_operation(input, operand, operation) click to toggle source
# File lib/liquid/standardfilters.rb, line 265
def apply_operation(input, operand, operation)
  result = to_number(input).send(operation, to_number(operand))
  result.is_a?(BigDecimal) ? result.to_f : result
end
to_number(obj) click to toggle source
# File lib/liquid/standardfilters.rb, line 252
def to_number(obj)
  case obj
  when Float
    BigDecimal.new(obj.to_s)
  when Numeric
    obj
  when String
    (obj.strip =~ /^\d+\.\d+$/) ? BigDecimal.new(obj) : obj.to_i
  else
    0
  end
end