EJS脚本和样式表部分

EJS脚本和样式表部分

本文介绍了EJS脚本和样式表部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来自ASP.NET MVC的node,express和EJS的新手。在Razor中,有一个名为 sections的功能,该功能使我可以在布局文件中为脚本和样式表之类的内容指定占位符,并在以后这些视图需要特定的脚本和样式表时在各自的视图中引用它们。这样,所有样式表都位于顶部,所有脚本都位于底部。 EJS中有类似的功能吗?

I'm new to node, express, and EJS, coming from ASP.NET MVC. In Razor, there is a feature called "sections" that allows me to specify a placeholder in my layout file for things like scripts and stylesheets, and reference them later in my individual views when those views need particular scripts and stylesheets. That way, all the stylesheets stay at the top, and all the scripts stay at the bottom. Is there a similar feature in EJS?

推荐答案

根据您的建议发布答案。

Posting as answer at your suggestion.

EJS是一个相当裸机的实现,尽管它确实具有局部性,但我认为它并不是干净地提供给您想要的东西,因为它并不是真正基于布局概念。您可能需要考虑将Jade作为模板引擎。它是的默认模板引擎,如果尚未使用,可能会很快使用它。

EJS is a fairly 'bare metal' implementation although it does have partials, I don't think it would cleanly give you what you want as it's not really modeled on a 'layout' concept. You might want to consider looking at Jade as a template engine. It is the default template engine for Express, which you will probably be using shortly if you aren't already.

个人而言,我发现我总体上非常喜欢Jade。只是因为它是如此简洁/简洁,所以在我眼中要容易得多。

Personally, I have found I really like Jade in general. It is much easier on my eyes simply because it is so terse/concise.

资源:






  • Jade on Github
  • Jade-lang.com
  • CSS Deck Jade Tutorial - Syntax
  • CSS Deck Jade Tutorial - Logic

使用 block 语法是我认为要描述的内容,但我认为在这些内容中没有很好的记录资源,但这是一个示例:

The use of the block syntax is what I think you want for what you describe, and I don't think it is well documented in those resources, but here's an example:

layout.jade

doctype html
html
  head
    title #{title} / #{version}
    link(rel='stylesheet', href='/css/site.css')
    script(src='/js/mygenericscript.js')
    block head

    body(class='foo')
        // the line below could be written as '#maincontent.bodyclass'
        div(id='maincontent', class='bodyclass')
          block content

template.jade

extends layout

block head
  // here I am including a page-specific script & stylesheet in the head section
  script(src='/js/mypagescript.js')
  link(rel='stylesheet', href='/css/crazypage.css')

block content
  div(class='page-specific-class')
    //etc, etc

这篇关于EJS脚本和样式表部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:01