当前位置: 首页 > 软件库 > Web应用开发 > >

express-ejs-layouts

Layout support for ejs in express.
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 凌华奥
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

express-ejs-layouts

Layout support for ejs in express

Installation

$ npm install express-ejs-layouts

Example

Check the example folder.

  1. git clone https://github.com/soarez/express-ejs-layouts.git
  2. cd express-ejs-layouts
  3. npm install
  4. node example
  5. Open http://localhost:3000/

Usage

var express = require('express');
var expressLayouts = require('express-ejs-layouts');

var app = express();

app.set('view engine', 'ejs');

app.use(expressLayouts);

app.get('/', function(req, res) {
  var locals = {
    title: 'Page Title',
    description: 'Page Description',
    header: 'Page Header'
  };
  res.render('the-view', locals);
});

app.listen(3000);

contentFor

A view

tyler
<%- contentFor('foo') %>
club
<%- contentFor('bar') %>
fight

With a layout

<%-bar%> <%-foo%>
<%-body%>

Renders

fight club
tyler

As another example, consider this view:

foo
<%- contentFor('pageSectionA') %>
bar
<%- contentFor('pageSectionB') %>
baz

Using it with this layout:

<div class="header"><%- pageSectionA %></div>
<div class="body"><%- body %></div>
<div class="footer"><%-defineContent('pageSectionB')%></div>

Will render:

<div class="header">bar</div>
<div class="body">foo</div>
<div class="footer">baz</div>

Notice that the difference between using <%- pageSectionA %> and <%-defineContent('pageSectionA')%> is that the former will generate an error if the view doesn't define content for this section.

Script blocks extraction

If you like to place all the script blocks at the end, you can do it like this:

app.set("layout extractScripts", true)

A view

something<script>somejs<script>something

With a layout

<body>
  <%- body %>
  <%- script %>
</body>

Renders

<body>
  somethingsomething
  <script>somejs<script>
</body>

Enabling invididually:

req.render('view', { extractScripts: true })

When the "layout extractScripts" option is activated, scripts defined in views will be extracted (won't be a part of body) and will be available for use in the layout through the variable scripts.

Another example:

This view:

<script src="/b.js" />
<div>foo</div>
<script src="/a.js" />
<div>bar</div>
<script src="/c.js" />

Used with this layout:

<div class="main">
<%- body %>
</div>
<!-- place the scripts at the end of the html page -->
<%- script %>

Will render:

<div class="main">
<div>foo</div>
<div>bar</div>
</div>
<!-- place the scripts at the end of the html page -->
<script src="/b.js" />
<script src="/a.js" />
<script src="/c.js" />

Style blocks extraction

Works exactly like script blocks extraction except:

  • Supported tags are <link rel="stylesheet" …> and <style …>
  • The option is named extractStyles
  • The template variable in layout is style

Meta blocks extraction

Works exactly like script blocks extraction except:

  • Supported tags are <meta …> and <meta …/>
  • The option is named extractMetas
  • The template variable in layout is meta

Set custom default layout

By default 'layout.ejs' is used. If you want to specify your customlayout (e.g. 'layouts/layout.ejs'), just set layout property inexpress app settings.

app.set('layout', 'layouts/layout');

Set custom layout for single render

Just pass layout as render locals object.

app.get('/', function(req, res) {
  res.render('the-view', { layout: 'specific-layout' });
});

Set no layout for single render

Just pass layout: false as render locals object.

app.get('/', function(req, res) {
  res.render('the-view', { layout: false });
);

Optional sections

In a layout, you can have optional sections using defineContent:Unspecified section content defaults to ''.

1
<%-defineContent('a')%>
2
<%-defineContent('b')%>
3

with a view:

<%- contentFor('a') %>
1.5

will render:

1
1.5
2
3

Running tests

Clone the repo and run:

$ npm test

License

MIT

  • http://blog.sina.com.cn/s/blog_ad0672d60101l2ml.html 1.express中使用ejs var express = require('express');//需要安装 express var app = express(); app.set("view engine","ejs");//模版引擎设置为 ejs 2.文件组织 在express中使用e

  • express中使用ejs var express = require('express');//需要安装 express : cnpm i --save express var app = express(); app.set("view engine","ejs");//模版引擎设置为 ejs 文件组织 在express中使用ejs,文件组织遵循express。 .views——-放置动态

  • 1.express中使用ejs var express = require('express');//需要安装 express var app = express(); app.set("view engine","ejs");//模版引擎设置为 ejs 2.文件组织 在express中使用ejs,文件组织遵循express。 .views-------放置动态模版 .public----

  • 我今天第一次使用,使用的时候,遇到一些问题,还好有朋友帮我一起解决; 我先说说我使用过程中遇到的问题; 在express框架中引用 app.set('views',__dirname + '/views'); app.set('view engine', 'ejs'); 路由中渲染 res.render('main/index',{ userInfo:req.userInfo }); 运行的时候老

  • 《Node.js开发指南》 项目地址 https://github.com/BYVoid/microblog 好不容易找到的基础版教程,但书中是基于express2的,而现在用的是express4了,本文主要介绍项目实现过程中遇到的各种问题及其解决方案;也是初学,所以有错的地方还望各位不吝指教, 项目基于express4,IDE用的是WebStorm,数据库是MongoDB,完全运行起来之后pac

 相关资料
  • 问题内容: 在我的index.ejs中,我有以下代码: 在我的节点中 但是,我在页面上获得 如果我写 我得到: 有没有一种方法可以传递 JS可读 的JSON ? 问题答案: 哦,那很容易,不要使用,而是使用。例如: 第一个将以HTML呈现,第二个将呈现变量(按原样评估)

  • 我正在节点服务器上使用express ejs在多视图应用程序中呈现视图。在客户端,我使用的是AngularJS。初始视图是一个登录页面,当使用时,该页面会正确显示。我想在用户成功连接时呈现一个新的视图,但是对于下一个,它仍然停留在登录页面上,尽管我在客户端接收到了正确的HTML。下面是我的相关代码: 部分文件结构: server.js Script.js login.ejs Home.ejs 这是

  • 本文向大家介绍详解node+express+ejs+bootstrap构建项目,包括了详解node+express+ejs+bootstrap构建项目的使用技巧和注意事项,需要的朋友参考一下 node+express+ejs+bootstrap是前端常用的项目结构,分享给大家,具体如下: 您可以通过node-express_jb51.rar 来克隆我创建好的项目结构,也可以通过下面的方式一步一步手

  • 我只是想安装node.js/express/ejs.我知道ejs不是真正的超文本标记语言,所以我很难只显示一个简单的图像。有人能给我指出正确的方向吗? 目录结构为: 我的应用程序/服务器。js myApp/视图/索引。ejs myApp/徽标。jpg 现在我有 我走错方向了吗?谢谢

  • EJS

    EJS 可以将数据和模板合并然后生成 HTML 文本。 示例代码: <!-- templates/supplies.ejs --><h1>Supplies</h1><ul><% for(var i=0; i<supplies.length; i++) {%>   <li><%= supplies[i] %></li><% } %></ul>var my_supplies = {supplies:

  • 问题内容: 我正在尝试按照https://stackoverflow.com/a/18633827/2063561的说明进行操作,但是仍然无法加载styles.css。 从app.js 在我的.ejs文件中,我尝试了这两行 都不加载css。我进入开发人员的控制台,注意到类型设置为’text / html’而不是’text / css’。 我的路看起来像 问题答案: 在server.js文件中使用它