Reputation: 1400
Right so I'm trying to do something dead simple, which is go through some files and print out the contents. This is my _plugins/test.rb
file:
module Jekyll
class TestPlugin < Liquid::Tag
def render(context)
Dir.glob("somefolder/*.someextension") do |my_file|
file = File.open(my_file)
contents = file.read
# print contents
end
end
end
end
Liquid::Template.register_tag('testplugin', Jekyll::TestPlugin)
Now considering the env, a simple puts contents
will output correct contents to the console. However, I want this to just spit out contents when called via {% testplugin %}
. I've tried the following combinations:
"contents"
#{contents}
#{@contents}
print contents
print "contents"
print #{contents}
print #{@contents}
None of which output anything when called via {% testplugin %}
. When I swap out my Dir.glob
stuff for something like "Hello there"
, the output will be correct. Needless to say I'm very unfamiliar with Ruby.
Upvotes: 1
Views: 272
Reputation: 66333
The issue here is that Dir.glob
when given a block (as you've done) will call the block once for each matching filename but then returns nil
.
This means that your render
method isn't actually returning anything.
One solution is to call glob
without a block. It will then return a list of the matching filenames and you can map this to the contents of the files.
For example to return the combined contents of all the matching files:
def render(context)
Dir.glob("somefolder/*.someextension").map do |filename|
File.read(filename)
end.join("\n")
end
Upvotes: 1