module Liquid::StandardFilters
Constants
- HTML_ESCAPE
- HTML_ESCAPE_ONCE_REGEXP
Public Instance Methods
absolute value
# File lib/liquid/standardfilters.rb, line 302 def abs(input) result = Utils.to_number(input).abs result.is_a?(BigDecimal) ? result.to_f : result end
add one string to another
# File lib/liquid/standardfilters.rb, line 223 def append(input, string) input.to_s + string.to_s end
capitalize words in the input centence
# File lib/liquid/standardfilters.rb, line 31 def capitalize(input) input.to_s.capitalize end
# File lib/liquid/standardfilters.rb, line 344 def ceil(input) Utils.to_number(input).ceil.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e.message end
Remove nils within an array provide optional property with which to check for nil
# File lib/liquid/standardfilters.rb, line 190 def compact(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.compact elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) ary.reject{ |a| a[property].nil? } end end
# File lib/liquid/standardfilters.rb, line 227 def concat(input, array) unless array.respond_to?(:to_ary) raise ArgumentError.new("concat filter requires an array argument") end InputIterator.new(input).concat(array) end
Reformat a date using Ruby's core Time#strftime( string ) -> string
%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 - Number of seconds since 1970-01-01 00:00:00 UTC. %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 See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
# File lib/liquid/standardfilters.rb, line 275 def date(input, format) return input if format.to_s.empty? return input unless date = Utils.to_date(input) date.strftime(format.to_s) end
# File lib/liquid/standardfilters.rb, line 356 def default(input, default_value = ''.freeze) if !input || input.respond_to?(:empty?) && input.empty? default_value else input end end
division
# File lib/liquid/standardfilters.rb, line 323 def divided_by(input, operand) apply_operation(input, operand, :/) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e.message end
convert an input string to DOWNCASE
# File lib/liquid/standardfilters.rb, line 21 def downcase(input) input.to_s.downcase end
# File lib/liquid/standardfilters.rb, line 35 def escape(input) CGI.escapeHTML(input).untaint unless input.nil? end
# File lib/liquid/standardfilters.rb, line 40 def escape_once(input) input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE) end
Get the first element of the passed in array
Example:
{{ product.images | first | to_img }}
# File lib/liquid/standardfilters.rb, line 288 def first(array) array.first if array.respond_to?(:first) end
# File lib/liquid/standardfilters.rb, line 350 def floor(input) Utils.to_number(input).floor.to_i rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e.message end
Join elements of the array with certain character between them
# File lib/liquid/standardfilters.rb, line 115 def join(input, glue = ' '.freeze) InputIterator.new(input).join(glue) end
Get the last element of the passed in array
Example:
{{ product.images | last | to_img }}
# File lib/liquid/standardfilters.rb, line 297 def last(array) array.last if array.respond_to?(:last) end
# File lib/liquid/standardfilters.rb, line 96 def lstrip(input) input.to_s.lstrip end
map/collect on a given property
# File lib/liquid/standardfilters.rb, line 175 def map(input, property) InputIterator.new(input).map do |e| e = e.call if e.is_a?(Proc) if property == "to_liquid".freeze e elsif e.respond_to?(:[]) r = e[property] r.is_a?(Proc) ? r.call : r end end end
subtraction
# File lib/liquid/standardfilters.rb, line 313 def minus(input, operand) apply_operation(input, operand, :-) end
# File lib/liquid/standardfilters.rb, line 329 def modulo(input, operand) apply_operation(input, operand, :%) rescue ::ZeroDivisionError => e raise Liquid::ZeroDivisionError, e.message end
Add <br /> tags in front of all newlines in input string
# File lib/liquid/standardfilters.rb, line 240 def newline_to_br(input) input.to_s.gsub(/\n/, "<br />\n".freeze) end
addition
# File lib/liquid/standardfilters.rb, line 308 def plus(input, operand) apply_operation(input, operand, :+) end
prepend a string to another
# File lib/liquid/standardfilters.rb, line 235 def prepend(input, string) string.to_s + input.to_s end
remove a substring
# File lib/liquid/standardfilters.rb, line 213 def remove(input, string) input.to_s.gsub(string.to_s, ''.freeze) end
remove the first occurrences of a substring
# File lib/liquid/standardfilters.rb, line 218 def remove_first(input, string) input.to_s.sub(string.to_s, ''.freeze) end
Replace occurrences of a string with another
# File lib/liquid/standardfilters.rb, line 203 def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string.to_s, replacement.to_s) end
Replace the first occurrences of a string with another
# File lib/liquid/standardfilters.rb, line 208 def replace_first(input, string, replacement = ''.freeze) input.to_s.sub(string.to_s, replacement.to_s) end
Reverse the elements of an array
# File lib/liquid/standardfilters.rb, line 169 def reverse(input) ary = InputIterator.new(input) ary.reverse end
# File lib/liquid/standardfilters.rb, line 335 def round(input, n = 0) result = Utils.to_number(input).round(Utils.to_number(n)) result = result.to_f if result.is_a?(BigDecimal) result = result.to_i if n == 0 result rescue ::FloatDomainError => e raise Liquid::FloatDomainError, e.message end
# File lib/liquid/standardfilters.rb, line 100 def rstrip(input) input.to_s.rstrip end
Return the size of an array or of an string
# File lib/liquid/standardfilters.rb, line 16 def size(input) input.respond_to?(:size) ? input.size : 0 end
# File lib/liquid/standardfilters.rb, line 52 def slice(input, offset, length = nil) offset = Utils.to_integer(offset) length = length ? Utils.to_integer(length) : 1 if input.is_a?(Array) input.slice(offset, length) || [] else input.to_s.slice(offset, length) || '' end end
Sort elements of the array provide optional property with which to sort an array of hashes or drops
# File lib/liquid/standardfilters.rb, line 121 def sort(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.sort elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? ary.sort do |a, b| a = a[property] b = b[property] if a && b a <=> b else a ? -1 : 1 end end end end
Sort elements of an array ignoring case if strings provide optional property with which to sort an array of hashes or drops
# File lib/liquid/standardfilters.rb, line 142 def sort_natural(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.sort { |a, b| a.casecmp(b) } elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? ary.sort { |a, b| a[property].casecmp(b[property]) } end end
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 88 def split(input, pattern) input.to_s.split(pattern.to_s) end
# File lib/liquid/standardfilters.rb, line 92 def strip(input) input.to_s.strip end
# File lib/liquid/standardfilters.rb, line 104 def strip_html(input) empty = ''.freeze input.to_s.gsub(/<script.*?<\/script>/m, empty).gsub(/<!--.*?-->/m, empty).gsub(/<style.*?<\/style>/m, empty).gsub(/<.*?>/m, empty) end
Remove all newlines from the string
# File lib/liquid/standardfilters.rb, line 110 def strip_newlines(input) input.to_s.gsub(/\r?\n/, ''.freeze) end
multiplication
# File lib/liquid/standardfilters.rb, line 318 def times(input, operand) apply_operation(input, operand, :*) end
Truncate a string down to x characters
# File lib/liquid/standardfilters.rb, line 64 def truncate(input, length = 50, truncate_string = "...".freeze) return if input.nil? input_str = input.to_s length = Utils.to_integer(length) truncate_string_str = truncate_string.to_s l = length - truncate_string_str.length l = 0 if l < 0 input_str.length > length ? input_str[0...l] + truncate_string_str : input_str end
# File lib/liquid/standardfilters.rb, line 74 def truncatewords(input, words = 15, truncate_string = "...".freeze) return if input.nil? wordlist = input.to_s.split words = Utils.to_integer(words) l = words - 1 l = 0 if l < 0 wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string.to_s : input end
Remove duplicate elements from an array provide optional property with which to determine uniqueness
# File lib/liquid/standardfilters.rb, line 156 def uniq(input, property = nil) ary = InputIterator.new(input) if property.nil? ary.uniq elsif ary.empty? # The next two cases assume a non-empty array. [] elsif ary.first.respond_to?(:[]) ary.uniq{ |a| a[property] } end end
convert an input string to UPCASE
# File lib/liquid/standardfilters.rb, line 26 def upcase(input) input.to_s.upcase end
# File lib/liquid/standardfilters.rb, line 48 def url_decode(input) CGI.unescape(input) unless input.nil? end
# File lib/liquid/standardfilters.rb, line 44 def url_encode(input) CGI.escape(input) unless input.nil? end
Private Instance Methods
# File lib/liquid/standardfilters.rb, line 366 def apply_operation(input, operand, operation) result = Utils.to_number(input).send(operation, Utils.to_number(operand)) result.is_a?(BigDecimal) ? result.to_f : result end