roger
roger

Reputation: 1263

Jekyll inheritance

I have this following layout, with jekyll 3.7.3;

declare A = apple

include file.html > declare A = orange

print A => orange

I am confused to how did A=orange leaked to the parent layout, in the jekyll's doc, says that the variables assess thru layout in the liquid tag. Does this apply to the include as well? It makes no sense in where child layout overwritten the parent layout, as it is saying in the github conversation and this conversation.

So my question is how is this inheritance work?

From what I understand about inheritance, there should be some control how the child variable be overwrite the parent variable. From the document, I believe it is through the variable layout. Then this should look like this;

declare A = apple 

include file.html > declare layout.A = orange

print A => apple

Other case is;

declare A = apple

include file.html > print A => apple

declare A = orange

print A => orange

What is the point of having an argument in the include if the child include inheritances the value without explicitly tells it to.

also with the leak variables into the include child, would mean the child include is not isolated any more for the purpose of special case like it is saying here

Upvotes: 0

Views: 757

Answers (1)

David Jacquel
David Jacquel

Reputation: 52819

Includes and layouts are not the same.

While generating a site, Jekyll does a lot of things in a particular order.

  • Read pages's datas
  • Render liquid
    • make necessary includes
    • compute liquid tags and filters
  • Render html in case the document is markdown
  • Render layout

When it renders liquid :

=== page.html
include other.html

print a
assign: a = apple
print a

include other.html
=== end page.html

Becomes a pile of code which is processed like this :

=== page.html
  ====== other.html
  print a ------> nil
  assign: a = orange
  print a ------> orange
  ====== end other.html
print a ------> orange
assign: a = apple
print a ------> apple
  ====== other.html
  print a ------> apple
  assign: a = orange
  print a ------> orange
  ====== end other.html
=== end page.html

Liquid tags are executed exactly in the order they appear in code, and variables (the local variables assigned in page body, not the one in front matter that are freezed and cannot be changed) are global and can be overridden from page or any child include.

After this, it renders HTML if necessary, and "spits" page's {{ content }} in the layout that knows nothing about page's local variables and can only see page's variables defined in front matter.

Upvotes: 2

Related Questions