jekyllプラグインでフィルタやタグを作成する方法を理解できないので、ディレクトリを返してその内容をループすることができます。私はこれらが見つかりました:Jekyllプラグインのディレクトリにあるファイルのリストを返しますか?
http://snippets.dzone.com/posts/show/302
これまでのところ、私が持っている:
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)
私が正常に文字列としてイメージのリストを返しますし、それを印刷することができます。
{% files test_string %}
私の人生のために、私はループすることはできませんどのように配列/ハッシュDir.globから返すに関係なく、配列、r。私はちょうど行うことができるようにしたい:私は、サイト上で使用することがあります様々なコレクションのために常に物事の配列を返すことができるようにする必要がありますするつもりです
{% for image in files %}
image
{% endfor %}
。私は構築するために、barebonesプラグインが必要です。
ありがとうございます!
更新:部分的に解決しました。この方法は機能しますが、endforの代わりにendloop_directoryを使用する必要があります。これは私にとっては醜いようです。また、フィルタは*。{jpg、png}のようなパラメータを取ることができません。なぜなら、htmlの{}をエスケープする方法がないからです。属性で正規表現の文字列を渡す方法についての提案を開いて...
#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)
です。 – jbranchaud