Tôi không thể tìm ra cách tạo bộ lọc hoặc thẻ trong plugin jekyll, để tôi có thể trả về một thư mục và lặp lại nội dung của nó. Tôi tìm thấy những:Trả về danh sách các tệp trong thư mục từ plugin Jekyll?
http://snippets.dzone.com/posts/show/302
Cho đến nay tôi có:
module Jekyll
class FilesTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
@text = text
end
def render(context)
#"#{@text} #{Time.now}"
Dir.glob("images/*").each { |i| "#{i}" }
#Dir.glob("images/*")
#Hash[*Dir.glob("images/*").collect { |v| [v, v*2] }.flatten]
end
end
end
Liquid::Template.register_tag('files', Jekyll::FilesTag)
tôi có thể trở thành công danh sách các hình ảnh như là một chuỗi và in nó với:
{% files test_string %}
Nhưng đối với cuộc sống của tôi, tôi không thể lặp lại ove r mảng, không có vấn đề làm thế nào tôi trở về mảng/băm từ Dir.glob. Tôi chỉ muốn có thể làm:
{% for image in files %}
image
{% endfor %}
Tôi sẽ cần phải có khả năng trả về mảng liên tục cho các bộ sưu tập khác nhau mà tôi sẽ sử dụng trên trang web. Tôi chỉ cần một plugin barebones để xây dựng.
Cảm ơn!
CẬP NHẬT: Tôi đã giải quyết một phần. Phương thức này hoạt động nhưng yêu cầu sử dụng endloop_directory thay vì endfor, điều này có vẻ hơi xấu với tôi. Ngoài ra, bộ lọc không thể lấy tham số như *. {Jpg, png} vì không có cách nào để thoát {} trong html. Mở để đề xuất về cách vượt qua chuỗi regex trong thuộc tính ...
#usage:
#{% loop_directory directory:images iterator:image filter:*.jpg sort:descending %}
# <img src="{{ image }}" />
#{% endloop_directory %}
module Jekyll
class LoopDirectoryTag < Liquid::Block
include Liquid::StandardFilters
Syntax = /(#{Liquid::QuotedFragment}+)?/
def initialize(tag_name, markup, tokens)
@attributes = {}
@attributes['directory'] = '';
@attributes['iterator'] = 'item';
@attributes['filter'] = 'item';
@attributes['sort'] = 'ascending';
# Parse parameters
if markup =~ Syntax
markup.scan(Liquid::TagAttributes) do |key, value|
@attributes[key] = value
end
else
raise SyntaxError.new("Bad options given to 'loop_directory' plugin.")
end
#if @attributes['directory'].nil?
# raise SyntaxError.new("You did not specify a directory for loop_directory.")
#end
super
end
def render(context)
context.registers[:loop_directory] ||= Hash.new(0)
images = Dir.glob(File.join(@attributes['directory'], @attributes['filter']))
if @attributes['sort'].casecmp("descending") == 0
# Find files and sort them reverse-lexically. This means
# that files whose names begin with YYYYMMDD are sorted newest first.
images.sort! {|x,y| y <=> x }
else
# sort normally in ascending order
images.sort!
end
length = images.length
result = []
context.stack do
images.each_with_index do |item, index|
context[@attributes['iterator']] = item
context['forloop'] =
{
'name' => @attributes['iterator'],
'length' => length,
'index' => index + 1,
'index0' => index,
'rindex' => length - index,
'rindex0' => length - index - 1,
'first' => (index == 0),
'last' => (index == length - 1)
}
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('loop_directory', Jekyll::LoopDirectoryTag)
Tôi đoán tất cả các phiếu bầu đều là vì đây là nhận xét chứ không phải là câu trả lời cho câu hỏi. – jbranchaud