如何使用jade模板将块附加到父级

Alx*_*Alx 10 inheritance append include node.js pug

我正在尝试使用jade模板创建模块化布局.我想将一个来自子节点的脚本块添加到其父节点父节点中.我不太确定它是否可能.

这是我的结构

layout.jade

head.jade

index.jade

users.jade

layout.jade:doctype html #html include head

    body
        block content
Run Code Online (Sandbox Code Playgroud)

head.jade:

head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    block scripts
Run Code Online (Sandbox Code Playgroud)

index.jade:

extends layout

block content
    h1 Hello

    include users
Run Code Online (Sandbox Code Playgroud)

users.jade

block append scripts
    script(src='/javascripts/user.js')  

ul
    each user, i in users
        li(class=i+"-"+user) #{user} 
Run Code Online (Sandbox Code Playgroud)

我想要的html输出应该是:

<!DOCTYPE html>
<html id="html">
    <head>
        <title>Index</title>
        <link href="/stylesheets/style.css" rel="stylesheet">
        <script src="/javascripts/user.js">  <!--// append this from user.jade into head.jade //-->
    </head>
    <body>

        <h1>Hello bob</h1>
        <li class="0-user1">user1</li>
Run Code Online (Sandbox Code Playgroud)

Pet*_*ons 4

这应该是可能的,jade 文档中有一些例子可以做到这一点。您的代码看起来一切都很好,除了您需要缩进script标记users.jade以使其缩进到block append script指令下方。

  • 尝试“附加脚本”而不是“阻止附加脚本”? (2认同)