【问题标题】:Inherited blocks are not processed不处理继承的块
【发布时间】:2018-02-22 13:26:53
【问题描述】:

使用 Hexo 和 Pug 作为模板引擎,我有一个主布局,我所有页面的默认布局 (layout.pug):

doctype html
html
    header
    body
        block nav
            p hello
        // logic of page decision goes here
        div This is the footer of the page

我想将nav 部分保存在一个单独的文件中(p hello 只是用于调试的占位符)所以我创建了nav.pug

extends layout

block nav
    div
        p --- navigation common to all pages ---

在构建网站时,我会系统地获取 HTML 输出

<!DOCTYPE 5>
<html>
<header></header>

<body>
    <p>hello</p>
    <!-- logic of page decision goes here-->
    <div>This is the footer of the page</div>
</body>

</html>

nav 块没有被替换,为什么?

带有--debug 的构建表明nav.pug 已处理:

PS D:\MegaSync\dev-perso\blog-vuetify\blog> hexo clean ; hexo g --debug
INFO  Deleted database.
INFO  Deleted public folder.
13:20:07.849 DEBUG Hexo version: 3.5.0
13:20:07.853 DEBUG Working directory: D:\MegaSync\dev-perso\blog-vuetify\blog\
13:20:07.975 DEBUG Config loaded: D:\MegaSync\dev-perso\blog-vuetify\blog\_config.yml
13:20:07.988 DEBUG Plugin loaded: hexo-generator-archive
(...)
13:20:08.367 DEBUG Plugin loaded: hexo-server
13:20:08.394 DEBUG Loading database.
13:20:08.401 INFO  Start processing
13:20:08.425 DEBUG Processed: _posts/second.md
13:20:08.427 DEBUG Processed: layout/index.pug
13:20:08.429 DEBUG Processed: layout/nav.pug      <--- HERE
13:20:08.431 DEBUG Processed: _posts/first.md
13:20:08.432 DEBUG Processed: layout/layout.pug
13:20:08.432 DEBUG Processed: layout/post.pug
13:20:08.503 DEBUG Generator: page
13:20:08.504 DEBUG Generator: post
13:20:08.505 DEBUG Generator: archive
13:20:08.505 DEBUG Generator: index
13:20:08.506 DEBUG Generator: category
13:20:08.506 DEBUG Generator: tag
13:20:08.507 DEBUG Generator: asset
13:20:08.510 INFO  Files loaded in 116 ms
13:20:08.514 DEBUG Rendering post: posts/second/index.html
13:20:08.521 DEBUG Rendering post: posts/first/index.html
13:20:08.523 DEBUG Rendering index: archives/index.html
13:20:08.525 DEBUG Rendering index: archives/2018/index.html
13:20:08.526 DEBUG Rendering index: archives/2018/02/index.html
13:20:08.528 DEBUG Rendering index: index.html
13:20:08.530 DEBUG Rendering index: tags/tech/index.html
13:20:08.699 INFO  Generated: index.html
13:20:08.702 INFO  Generated: archives/index.html
13:20:08.704 INFO  Generated: posts/second/index.html
13:20:08.705 INFO  Generated: posts/first/index.html
13:20:08.706 INFO  Generated: archives/2018/02/index.html
13:20:08.708 INFO  Generated: archives/2018/index.html
13:20:08.709 INFO  Generated: tags/tech/index.html
13:20:08.711 INFO  7 files generated in 201 ms
13:20:10.562 DEBUG Database saved

【问题讨论】:

    标签: pug hexo


    【解决方案1】:

    默认情况下,Hexo 使用layout.[pug|ejs|etc] 作为定义站点结构的父模板,如果您尝试使用其他任何内容,它将被忽略。 Hexo 处理资产或模板文件夹中的所有内容,包括实际上不对应页面的模板。

    如果你真的想以这种方式使用 extends,你必须创建另一个 Pug 模板,你将在 layout.pug 中扩展,所以你当前在 nav.pug 中的代码必须进入 @ 987654325@。

    不过,你最好还是把导航变成一个局部,你也可以添加一些条件逻辑(例如:访问 Hexo 的全局 path 变量来查看给定的链接是否匹配路径并相应地应用样式)。然后您可以改用include nav.pug

    例如,你可以这样做。

    layout.pug

    doctype html
    html
        header
        body
            block nav
                p hello
            include nav.pug
            // logic of page decision goes here
            div This is the footer of the page
    

    nav.pug

    - items = ['archive', 'foo', 'home']
    nav
      for i in items 
        a.link(href='/' + i, class="" + path.includes(i) ? 'active' : '')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 2017-01-28
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多