Override first and last filters to allow simple array slicing
This commit is contained in:
parent
9967b5cdf2
commit
31620d0c0a
@ -44,6 +44,26 @@ module ChiliProject
|
|||||||
def replace_first(input, string, replacement = '')
|
def replace_first(input, string, replacement = '')
|
||||||
input.to_s.sub(string){replacement}
|
input.to_s.sub(string){replacement}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Get the first element(s) of the passed in array
|
||||||
|
# Example:
|
||||||
|
# {{ product.images | first | to_img }}
|
||||||
|
def first(array, count=nil)
|
||||||
|
return array.first if count.nil? && array.respond_to?(:first)
|
||||||
|
if array.respond_to?(:[])
|
||||||
|
count.to_i > 0 ? array[0..count.to_i-1] : []
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get the last element(s) of the passed in array
|
||||||
|
# Example:
|
||||||
|
# {{ product.images | last | to_img }}
|
||||||
|
def last(array, count=nil)
|
||||||
|
array.last if count=nil? && array.respond_to?(:last)
|
||||||
|
if array.respond_to?(:[])
|
||||||
|
count.to_i > 0 ? array[(count.to_i * -1)..-1] : []
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Filters
|
module Filters
|
||||||
|
Loading…
x
Reference in New Issue
Block a user