简体   繁体   English

如何在Jade中创建一个可重复使用的标记

[英]How can I create a re-usable piece of markup in Jade

What I'm trying to accomplish. 我正在努力实现的目标。

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. 我想要做的实际上非常简单,Jade模板引擎应该可以帮助我很多,但我遇到了一些障碍。

I'm building a site that uses a lot of semi-transparent elements like the one in this jsFiddle: http://jsfiddle.net/Chevex/UfKnM/ 我正在建立一个使用大量半透明元素的网站,比如这个jsFiddle: http//jsfiddle.net/Chevex/UfKnM/
In order to make the container background be semi-transparent but keep the text opaque this involves 3 elements: 为了使容器背景半透明但保持文本不透明,这涉及3个元素:

  • A container DIV with position: relative 容器DIV, position: relative
  • A child DIV with position: absolute , a background color, height/width set to 100%, and its opacity set to the desired level. 具有position: absolute ,背景颜色,高度/宽度设置为100%以及其不透明度设置为所需级别的子DIV。
  • Another child DIV for the content with no special positioning. 另一个孩子DIV的内容没有特别定位。

It's pretty simple and I use it fairly effectively on CodeTunnel.com . 它非常简单,我在CodeTunnel.com上使用它非常有效。

How I want to simplify it. 我想如何简化它。

I'm re-writing CodeTunnel.com 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. 我正在node.js中重写CodeTunnel.com,而Jade模板引擎似乎可以大大简化我一遍又一遍重复使用的这个标记。 Jade mixins look promising so here's what I did: Jade mixins看起来很有希望,所以这就是我做的:

  1. I defined a mixin so I could just use it wherever I need it. 我定义了一个mixin,所以我可以在任何需要它的地方使用它。

     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: 使用mixin,传入一个内容块:

     +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. 我想在layout.jade模板中使用这个mixin,我希望子模板能够使用块继承。 My layout.jade file looks like this: 我的layout.jade文件如下所示:

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: 然后在另一个jade文件(index.jade)中我扩展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. 我假设它与block关键字冲突有关。 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. mixin block内部是一个隐式参数,包含传递给mixin的块,但是当扩展jade模板块时,是一个关键字,用于标识要在父模板中的等效块中替换的标记块。

If I replace the block bodyContent that I'm passing into the mixin with any other markup then everything works fine. 如果我用任何其他标记替换我传入mixin的block bodyContent ,那么一切正常。 It's only when I try to pass in a block definition that it gets upset. 只有当我尝试传递一个块定义时才会感到沮丧。

Any ideas? 有任何想法吗?

I'd suspect that, because mixins define their own functions , the block bodyContent is being defined in different scope inaccessible from index.jade . 我怀疑,因为mixins定义了自己的函数 ,所以block bodyContent被定义在index.jade无法访问的不同范围内。

What you can try instead is to move the use of the mixin to the inheriting view since mixins are "hoisted" : 你可以尝试的是将mixin的使用转移到继承视图,因为mixins被“提升”

layout.jade : layout.jade

doctype 5

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

html
    head
        title Container mixin text
    body
        block bodyContent

index.jade : index.jade

extends layout

block bodyContent
    +container#myContainer
        h1 Some test content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM