2012-03-22 12 views
9

jekyllプラグインでフィルタやタグを作成する方法を理解できないので、ディレクトリを返してその内容をループすることができます。私はこれらが見つかりました:Jekyllプラグインのディレクトリにあるファイルのリストを返しますか?

http://pastebin.com/LRfMVN5Y

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) 

答えて

-4

あなたはジキルを使用している具体的な理由はありますか? JekyllはフラットなHTMLファイルを生成するように設計されていますが、あなたはもっとダイナミックなものを求めているようです。

Sinatraのようなものを使用すると、ファイルのリストを取得してテンプレート内で繰り返し処理するのが非常に簡単です。

+1

です。 – jbranchaud

0

Jigyll 1.0.0betaにマージされるのを待っているこの機能のGithub Master Branchにプルリクエストがあります。彼らはクリエイターTPWからの最終承認を待っているだけです。

コードを表示して、自分で使用するためにコピーして、マージされるタイミングを見守ることができます。次に、実行することにより、その機能と合併ジキルをダウンロードして、プラグインなしでそれを使用することができます:GitHubのからあなたのエッジバージョンを取得します

gem install jekyll --pre

を。

ここでPRだ - ファイルを一覧表示するための新しいリキッドタグ:ディレクトリ:

https://github.com/mojombo/jekyll/pull/585

1

私はここにプラグインが見つかりました:How to list files in a directory with Liquid?トリックを行う可能性があります:

ジキル:: DirectoryTagが このタグを使用すると、特定のパスのファイルを反復処理できます。ディレクトリタグはファイルオブジェクトとforloopオブジェクトを生成します。ファイルが標準Jekyll形式、YYYY-MM-DD-file-titleに準拠している場合、それらの属性はそのファイルオブジェクトに設定されます。これはコメントしていない質問への答えであるので、私はすべてのダウンの票を推測してい

https://github.com/sillylogger/jekyll-directory

関連する問題