最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I create a re-usable piece of markup in Jade - Stack Overflow

programmeradmin2浏览0评论

What I'm trying to acplish.

What I'm trying to do is actually really simple and the Jade template engine should be able to help me out quite a bit with it, but I'm running into some snags.

I'm building a site that uses a lot of semi-transparent elements like the one in this jsFiddle: /
In order to make the container background be semi-transparent but keep the text opaque this involves 3 elements:

  • A container DIV with position: relative
  • A child DIV with position: absolute, a background color, height/width set to 100%, and its opacity set to the desired level.
  • Another child DIV for the content with no special positioning.

It's pretty simple and I use it fairly effectively on CodeTunnel.

How I want to simplify it.

I'm re-writing CodeTunnel in node.js and the Jade template engine seems like it could greatly simplify this piece of markup that I re-use over and over again. Jade mixins look promising so here's what I did:

  1. I defined a mixin so I could just use it wherever I need it.

    mixin container
        .container(id=attributes.id) // attributes is an implicit argument that contains any attributes passed in.
            .translucentFrame
            .contentFrame
                block // block is an implicit argument that contains all content from the block passed into the mixin.
    
  2. Use the mixin, passing in a block of content:

    +container#myContainer
        h1 Some test content
    

    Generates:

    <div id="myContainer" class="container">
        <div class="translucentFrame"></div>
        <div class="contentFrame">
            <h1>Some test content</h1>
        </div>
    </div>
    

So far everything works great! There is just one problem. I want to use this mixin in a layout.jade template and I want the child template to be able to use block inheritance. My layout.jade file looks like this:

doctype 5
mixin container
    .container(id=attributes.id)
        .translucentFrame
        .contentFrame
            block
html
    head
        title Container mixin text
    body
        +container#bodyContent
            block bodyContent

Then in another jade file (index.jade) I extend layout.jade:

extends layout

block bodyContent
    h1 Some test Content

Everything looks to be in order but the jade parser fails:

I assume it has something to do with the block keyword conflicting. Inside a mixin block is an implicit argument containing the block passed into the mixin, but when extending a jade template block is a keyword that identifies a block of markup that is to be substituted in the equivalent block in the parent template.

If I replace the block bodyContent that I'm passing into the mixin with any other markup then everything works fine. It's only when I try to pass in a block definition that it gets upset.

Any ideas?

What I'm trying to acplish.

What I'm trying to do is actually really simple and the Jade template engine should be able to help me out quite a bit with it, but I'm running into some snags.

I'm building a site that uses a lot of semi-transparent elements like the one in this jsFiddle: http://jsfiddle/Chevex/UfKnM/
In order to make the container background be semi-transparent but keep the text opaque this involves 3 elements:

  • A container DIV with position: relative
  • A child DIV with position: absolute, a background color, height/width set to 100%, and its opacity set to the desired level.
  • Another child DIV for the content with no special positioning.

It's pretty simple and I use it fairly effectively on CodeTunnel..

How I want to simplify it.

I'm re-writing CodeTunnel. in node.js and the Jade template engine seems like it could greatly simplify this piece of markup that I re-use over and over again. Jade mixins look promising so here's what I did:

  1. I defined a mixin so I could just use it wherever I need it.

    mixin container
        .container(id=attributes.id) // attributes is an implicit argument that contains any attributes passed in.
            .translucentFrame
            .contentFrame
                block // block is an implicit argument that contains all content from the block passed into the mixin.
    
  2. Use the mixin, passing in a block of content:

    +container#myContainer
        h1 Some test content
    

    Generates:

    <div id="myContainer" class="container">
        <div class="translucentFrame"></div>
        <div class="contentFrame">
            <h1>Some test content</h1>
        </div>
    </div>
    

So far everything works great! There is just one problem. I want to use this mixin in a layout.jade template and I want the child template to be able to use block inheritance. My layout.jade file looks like this:

doctype 5
mixin container
    .container(id=attributes.id)
        .translucentFrame
        .contentFrame
            block
html
    head
        title Container mixin text
    body
        +container#bodyContent
            block bodyContent

Then in another jade file (index.jade) I extend layout.jade:

extends layout

block bodyContent
    h1 Some test Content

Everything looks to be in order but the jade parser fails:

I assume it has something to do with the block keyword conflicting. Inside a mixin block is an implicit argument containing the block passed into the mixin, but when extending a jade template block is a keyword that identifies a block of markup that is to be substituted in the equivalent block in the parent template.

If I replace the block bodyContent that I'm passing into the mixin with any other markup then everything works fine. It's only when I try to pass in a block definition that it gets upset.

Any ideas?

Share Improve this question edited Dec 17, 2012 at 23:48 ChevCast asked Dec 17, 2012 at 23:41 ChevCastChevCast 59.3k66 gold badges221 silver badges325 bronze badges 3
  • Has anything changed since you originally posted this? i.e. does this work now, or is there a better approach ? – Codebling Commented Nov 23, 2015 at 0:20
  • @CodeBling I don't believe much has changed here. – ChevCast Commented Nov 24, 2015 at 15:45
  • That's too bad. Seems like something that will be solved in Jade 2, which should be released soonish. – Codebling Commented Nov 24, 2015 at 21:01
Add a ment  | 

1 Answer 1

Reset to default 6

I'd suspect that, because mixins define their own functions, the block bodyContent is being defined in different scope inaccessible from index.jade.

What you can try instead is to move the use of the mixin to the inheriting view since mixins are "hoisted":

layout.jade:

doctype 5

mixin container
    .container(id=attributes.id)
        .translucentFrame
        .contentFrame
            block

html
    head
        title Container mixin text
    body
        block bodyContent

index.jade:

extends layout

block bodyContent
    +container#myContainer
        h1 Some test content
发布评论

评论列表(0)

  1. 暂无评论