Module: Liquid::StandardFilters
- Defined in:
- lib/liquid/standardfilters.rb
Instance Method Summary collapse
-
#date(input, format) ⇒ Object
Reformat a date.
-
#downcase(input) ⇒ Object
convert a input string to DOWNCASE.
-
#first(array) ⇒ Object
Get the first element of the passed in array .
-
#join(input, glue = ' ') ⇒ Object
Join elements of the array with certain character between them.
-
#last(array) ⇒ Object
Get the last element of the passed in array .
-
#size(input) ⇒ Object
Return the size of an array or of an string.
-
#sort(input) ⇒ Object
Sort elements of the array.
- #strip_html(input) ⇒ Object
-
#truncate(input, characters = 100) ⇒ Object
Truncate a string down to x characters.
-
#truncatewords(input, words = 15) ⇒ Object
Truncate string down to x words.
-
#upcase(input) ⇒ Object
convert a input string to UPCASE.
Instance Method Details
#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
82 83 84 85 86 87 88 |
# File 'lib/liquid/standardfilters.rb', line 82 def date(input, format) date = input date = Time.parse(input) if input.is_a?(String) date.strftime(format) rescue => e input end |
#downcase(input) ⇒ Object
convert a input string to DOWNCASE
12 13 14 |
# File 'lib/liquid/standardfilters.rb', line 12 def downcase(input) input.downcase rescue input end |
#first(array) ⇒ Object
Get the first element of the passed in array
Example:
{{ product.images | first | to_img }}
95 96 97 |
# File 'lib/liquid/standardfilters.rb', line 95 def first(array) array.first if array.respond_to?(:first) end |
#join(input, glue = ' ') ⇒ Object
Join elements of the array with certain character between them
45 46 47 |
# File 'lib/liquid/standardfilters.rb', line 45 def join(input, glue = ' ') [input].flatten.join(glue) end |
#last(array) ⇒ Object
Get the last element of the passed in array
Example:
{{ product.images | last | to_img }}
104 105 106 |
# File 'lib/liquid/standardfilters.rb', line 104 def last(array) array.last if array.respond_to?(:last) end |
#size(input) ⇒ Object
Return the size of an array or of an string
6 7 8 9 |
# File 'lib/liquid/standardfilters.rb', line 6 def size(input) input.respond_to?(:size) ? input.size : 0 end |
#sort(input) ⇒ Object
Sort elements of the array
50 51 52 |
# File 'lib/liquid/standardfilters.rb', line 50 def sort(input) [input].flatten.sort end |
#strip_html(input) ⇒ Object
40 41 42 |
# File 'lib/liquid/standardfilters.rb', line 40 def strip_html(input) input.to_s.gsub(/<.*?>/, '') end |
#truncate(input, characters = 100) ⇒ Object
Truncate a string down to x characters
22 23 24 25 26 27 28 |
# File 'lib/liquid/standardfilters.rb', line 22 def truncate(input, characters = 100) if input.to_s.size > characters.to_i input.to_s[0..characters.to_i] + '…' else input end end |
#truncatewords(input, words = 15) ⇒ Object
Truncate string down to x words
31 32 33 34 35 36 37 38 |
# File 'lib/liquid/standardfilters.rb', line 31 def truncatewords(input, words = 15) wordlist = [input.to_s.split].flatten if wordlist.size > words.to_i wordlist[0..words.to_i].join(' ') + '…' else input end end |
#upcase(input) ⇒ Object
convert a input string to UPCASE
17 18 19 |
# File 'lib/liquid/standardfilters.rb', line 17 def upcase(input) input.upcase rescue input end |