Jekyll Page data not saved

Jekyll version 2.5.3

We are trying to write a plugin to advertise content that is agnostic as to whether its page or message. However, there is some difference between pages and posts that are not clear.

# this does not work site.pages.each do |page| # this does not show up in liquid page.data['baz'] = 'Foo' end # this does work site.posts.each do |post| # this does show up in liquid post.data['baz'] = 'Bar' end 

Any ideas as to why the .data hash mutation for messages is available in liquid, but the .data hash mutation for pages has no effect?

We also tested this with .data["title"] , and this affects posts , but page captions are not displayed.


 {% for post in site.posts %} <div class="postbaz">{{ post.baz }}</div> {% endfor %} {% for page in site.pages %} <div class="pagebaz">{{ page.baz }}</div> {% endfor %} 

gives

 <div class="postbaz">Foo</div> <div class="postbaz">Foo</div> <div class="postbaz">Foo</div> <div class="pagebaz"></div> <div class="pagebaz"></div> 
+5
source share
1 answer

Something is missing for you. Adding values ​​to data works great for both pages and posts.

See my repository for reference.

Here I add the attribute "foo" to all pages and posts

_plugins / foo.rb

 module Foo class Generator < Jekyll::Generator def generate(site) puts "Our plugin is running" puts site.pages.inspect puts site.posts.inspect site.pages.each do |page| page.data['foo'] = 'bar' end site.posts.each do |post| post.data['foo'] = 'bar' end end end end 

Here I add "foo" to the mailing list layout:

post.html

 <article class="post-content"> {{ content }} {{ page.foo }} </article> 

And page layout :

page.html

 <article class="post-content"> {{ content }} {{ page.foo }} </article> 

After running jekyll b I can see the output in post and the page where I expect them.

In a separate branch, I re-created your setting, where you iterate over all pages and posts:

default.html ( source )

 {% for post in site.posts %} <div class="postbaz">{{ post.foo }}</div> {% endfor %} {% for page in site.pages %} <div class="pagebaz">{{ page.foo }}</div> {% endfor %} 

_plugins / foo.rb ( source )

 site.pages.each do |page| page.data['foo'] = 'bar' end site.posts.each do |post| post.data['foo'] = 'baz' end 

(Note that the bar property is for the page and baz is for the message.)

It displays as expected:

site / index.html ( source )

 <div class="postbaz">baz</div> <div class="pagebaz">bar</div> <div class="pagebaz">bar</div> <div class="pagebaz">bar</div> <div class="pagebaz">bar</div> 
+1
source

All Articles