I can't figure out how to create a filter or tag in a jekyll plugin, so that I can return a directory and loop over its contents. I found these:
我无法弄清楚如何在jekyll插件中创建过滤器或标记,以便我可以返回一个目录并循环其内容。我找到了这些:
http://pastebin.com/LRfMVN5Y
http://snippets.dzone.com/posts/show/302
http://snippets.dzone.com/posts/show/302
So far I have:
到目前为止我有:
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)
I can successfully return the list of images as a string and print it with:
我可以成功地将图像列表作为字符串返回并打印出来:
{% files test_string %}
But for the life of me, I can't loop over the array, no matter how I return the array/hash from Dir.glob. I just want to be able to do:
但是对于我的生活,无论我如何从Dir.glob返回数组/哈希,我都无法遍历数组。我只是希望能够做到:
{% for image in files %}
image
{% endfor %}
I'm going to need to be able to return arrays of things constantly for the various collections I'll be using on the site. I just need a barebones plugin to build upon.
我将需要能够为我将在网站上使用的各种集合不断返回数组。我只需要一个准系统插件来构建。
Thanks!
谢谢!
UPDATE: I partially solved it. This method works but requires using endloop_directory instead of endfor, which seems a bit ugly to me. Also, the filter is unable to take a parameter like *.{jpg,png} because there is no way to escape the {} in the html. Open to suggestions on how to pass a regex string in an attribute...
更新:我部分解决了它。这个方法有效但需要使用endloop_directory而不是endfor,这对我来说似乎有点难看。此外,过滤器无法获取像*。{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)
3 个解决方案
#1
1
I found a plugin here: How to list files in a directory with Liquid? that might do the trick:
我在这里找到了一个插件:如何用Liquid列出目录中的文件?这可能会成功:
Jekyll::DirectoryTag This tag lets you iterate over files at a particular path. The directory tag yields a file object and a forloop object. If files conform to the standard Jekyll format, YYYY-MM-DD-file-title, then those attributes will be populated on that file object.
Jekyll :: DirectoryTag此标记允许您在特定路径上迭代文件。目录标记产生文件对象和forloop对象。如果文件符合标准的Jekyll格式YYYY-MM-DD-file-title,那么这些属性将填充在该文件对象上。
https://github.com/sillylogger/jekyll-directory
https://github.com/sillylogger/jekyll-directory
#2
0
There is a Pull Request on the Github Master Branch for this feature waiting to be merged into the Jekyll 1.0.0beta; they are just waiting for final approval from the creator TPW.
在Github Master Branch上有一个Pull Request,这个功能等待合并到Jekyll 1.0.0beta中;他们只是在等待创作者TPW的最终批准。
You can view the code and copy it for your own use now, and keep an eye out for when in gets merged. Then you can download the merged Jekyll with that feature and use it without a plugin, by doing:
您现在可以查看代码并将其复制以供自己使用,并留意何时合并。然后,您可以使用该功能下载合并的Jekyll,并在没有插件的情况下使用它,方法是:
gem install jekyll --pre
gem install jekyll --pre
Which will get you the edge version from Github.
这将从Github获得优势。
Here's the PR - New Liquid tag for listing files: directory:
这是用于列出文件的PR - New Liquid标签:目录:
https://github.com/mojombo/jekyll/pull/585
https://github.com/mojombo/jekyll/pull/585
#3
-4
Is there a specific reason you're using Jekyll? It seems like you want something more dynamic, whereas Jekyll is designed to generate flat HTML files.
您使用Jekyll的具体原因是什么?看起来你想要更动态的东西,而Jekyll则设计用于生成平面HTML文件。
You might be happier using something like Sinatra, where you can do something very straightforward to grab the list of files and iterate over them in a template.
你可能会更喜欢使用像Sinatra这样的东西,在那里你可以做一些非常简单的事情来获取文件列表并在模板中迭代它们。
#1
1
I found a plugin here: How to list files in a directory with Liquid? that might do the trick:
我在这里找到了一个插件:如何用Liquid列出目录中的文件?这可能会成功:
Jekyll::DirectoryTag This tag lets you iterate over files at a particular path. The directory tag yields a file object and a forloop object. If files conform to the standard Jekyll format, YYYY-MM-DD-file-title, then those attributes will be populated on that file object.
Jekyll :: DirectoryTag此标记允许您在特定路径上迭代文件。目录标记产生文件对象和forloop对象。如果文件符合标准的Jekyll格式YYYY-MM-DD-file-title,那么这些属性将填充在该文件对象上。
https://github.com/sillylogger/jekyll-directory
https://github.com/sillylogger/jekyll-directory
#2
0
There is a Pull Request on the Github Master Branch for this feature waiting to be merged into the Jekyll 1.0.0beta; they are just waiting for final approval from the creator TPW.
在Github Master Branch上有一个Pull Request,这个功能等待合并到Jekyll 1.0.0beta中;他们只是在等待创作者TPW的最终批准。
You can view the code and copy it for your own use now, and keep an eye out for when in gets merged. Then you can download the merged Jekyll with that feature and use it without a plugin, by doing:
您现在可以查看代码并将其复制以供自己使用,并留意何时合并。然后,您可以使用该功能下载合并的Jekyll,并在没有插件的情况下使用它,方法是:
gem install jekyll --pre
gem install jekyll --pre
Which will get you the edge version from Github.
这将从Github获得优势。
Here's the PR - New Liquid tag for listing files: directory:
这是用于列出文件的PR - New Liquid标签:目录:
https://github.com/mojombo/jekyll/pull/585
https://github.com/mojombo/jekyll/pull/585
#3
-4
Is there a specific reason you're using Jekyll? It seems like you want something more dynamic, whereas Jekyll is designed to generate flat HTML files.
您使用Jekyll的具体原因是什么?看起来你想要更动态的东西,而Jekyll则设计用于生成平面HTML文件。
You might be happier using something like Sinatra, where you can do something very straightforward to grab the list of files and iterate over them in a template.
你可能会更喜欢使用像Sinatra这样的东西,在那里你可以做一些非常简单的事情来获取文件列表并在模板中迭代它们。