【问题标题】:Jekyll inheritance杰基尔继承
【发布时间】:2018-03-16 12:16:24
【问题描述】:

我有以下布局,使用 jekyll 3.7.3;

declare A = apple

include file.html > declare A = orange

print A => orange

我很困惑A=orange 是如何泄漏到父布局的,jekyll's doc 表示变量通过液体标签中的layout 进行评估。这是否也适用于include?正如github conversationconversation 中所说的那样,子布局覆盖父布局的位置没有任何意义。

所以我的问题是这种继承是如何工作的?

根据我对继承的理解,应该有一些控制子变量如何覆盖父变量。从文档来看,我相信是通过变量layout。那么这应该是这样的;

declare A = apple 

include file.html > declare layout.A = orange

print A => apple

其他情况是;

声明 A = 苹果

include file.html > print A => 苹果

声明 A = 橙色

打印 A => 橙色

如果子 include 继承了值而没有明确告诉它,那么在包含中包含参数有什么意义。

还有泄漏变量到include 孩子,这意味着孩子include 不再被隔离,因为它是saying here 的特殊情况

【问题讨论】:

    标签: jekyll jekyll-paginator


    【解决方案1】:

    包含和布局不一样。

    在生成网站时,Jekyll 会按照特定的顺序做很多事情。

    • 读取页面数据
    • 渲染液体
      • 必要的includes
      • 计算液体标签和过滤器
    • 在文档被降价的情况下呈现 html
    • 渲染布局

    当它呈现液体时:

    === page.html
    include other.html
    
    print a
    assign: a = apple
    print a
    
    include other.html
    === end page.html
    

    变成一堆这样处理的代码:

    === 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 标签完全按照它们在代码中出现的顺序执行,变量(在页面正文中分配的局部变量,而不是前面的那个被冻结且无法更改的变量)是全局变量并且可以从页面或任何子包含覆盖。

    在此之后,如有必要,它会渲染 HTML,并在布局中“吐出”页面的 {{ content }},而该布局对页面的局部变量一无所知,只能看到在前面定义的页面变量。

    【讨论】:

    • 感谢您对此的解释,现在人们在 GitHub 上对 Code Pattern 的看法更加清晰。与其将每个 include 视为一个单独的实体或闭包函数,不如将它们视为较大部分的一小部分并在布局之前进行渲染。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多