Skip to content
Snippets Groups Projects
Commit 31620d0c authored by Holger Just's avatar Holger Just
Browse files

Override first and last filters to allow simple array slicing

parent 9967b5cd
Tags
No related merge requests found
......@@ -44,6 +44,26 @@ module ChiliProject
def replace_first(input, string, replacement = '')
input.to_s.sub(string){replacement}
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
module Filters
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment