简体   繁体   English

Node.js,Pug模板

[英]Node.js, pug templates

Very very new to using the above and so far so good. 到目前为止,使用上述内容非常新。 I'm just doing a simple test site to get my head around things. 我只是在做一个简单的测试站点,以使我了解一切。

Got a single template and two pages (index & about). 只有一个模板和两页(索引和大约)。 What I cannot figure out and I've read various website on the matter, is how I can have different content for the two pages using the single template. 我不知道并且已阅读有关此内容的各种网站,是如何使用一个模板在两个页面上具有不同的内容。 I've probably not got something right or doing things completely wrong, so if anyone can point me in the right direction or provide good working examples. 我可能做错了什么,或者做的事情完全错了,所以如果有人可以指出正确的方向或提供良好的工作示例。 it would help me no end. 这对我没有尽头。

The Template 模板

doctype html
html(lang="en")
head
    title= metaTitle
    meta(charset='utf-8')
    meta(http-equiv="X-UA-Compatible", content="IE=edge")
    meta(name="viewport", content="width=device-width, initial-scale=1")
    link(rel='stylesheet', href='http://fonts.googleapis.com/css?family=Open+Sans')
    link(rel='stylesheet', href='_templates/bootstrap336/css/bootstrap.css')
    link(rel='stylesheet', href='_templates/css/generic.css')

body
    .container
        header
            #header
                h1 Node.js

        nav.navbar.navbar-default
            include shared/nav.pug

        section

            h3 #{pageHeading}

            <!-- Want my content here -->

            p 
                img(src='/_templates/images/reg_icon.png')

        footer
            .row
                .col-xs-12.col-sm-6
                    Copyright &copy; 2016
                .col-xs-12.col-sm-6.text-right
                    Privacy

    script(src='_includes/jquery/jquery-2.2.3.min.js')
    script(src='_includes/jquery/jquery-ui-1114/jquery-ui.js')
    script(src='_templates/bootstrap336/js/bootstrap.min.js')

Basic webserver 基本的网络服务器

//Basic webserver
var express = require('express'),
app = express();

require('./routes')(app);

module.exports=app;

//config
app.set('view engine','pug');
app.set('views',__dirname + '/public/_templates');

//standard
app.use(express.static(__dirname + '/public'));

//Starts and listens
var port = process.env.PORT || 3000;
app.listen(port, function() {
    console.log("Listening on " + port+" | In folder " + __dirname + '\\public');
})

My routes.js file 我的routes.js文件

module.exports = function(app){

var coretitle="Node.js :: Test";

app.get('/', function (req, res) {
    res.render('index', {
        metaTitle   :   coretitle,
        pageHeading :   'First attempt'
    });
});

app.get('/about', function (req, res) {
    res.render('index', {
        metaTitle   :   coretitle,
        pageHeading :   'All About This'
    });
});

}

It's really simple, you just need to use the extends syntax in your files. 这非常简单,您只需要在文件中使用扩展语法即可。 I will write an small example :). 我会写一个小例子:)。

layout.pug layout.pug

//Here you define your basic html template, and would be as follow //在这里定义基本的html模板,如下所示

doctype html  
html
  head
    title Pug Example
    meta(name="viewport" content="width=device-width, initial-scale=1")
    link(rel="stylesheet" href="/components/bootstrap/dist/css/bootstrap.min.css")
    link(rel="stylesheet" href="/css/payment.css")
    script(src="/components/jquery/dist/jquery.min.js")
    script(src="/components/bootstrap/dist/js/bootstrap.min.js")  

  body
    div.container
     block content

Notice, in this file I've just included the basic HTML5 skeleton of your page. 注意,在此文件中,我仅包含了页面的基本HTML5框架。 Then in your about and index page you can proceed as follow. 然后,在“关于”和“索引”页面中,您可以按照以下步骤进行操作。

index.pug index.pug

extends layout
block content
  .row
    .col-md-8
      p Some info right here :D

NOTE: The extends will look in your directory for layout.pug, in my case the templates are in the same level,aka the same directory, but if you have the following folder structure like: 注意: 扩展名将在您的目录中查找layout.pug,在我的情况下,模板位于同一级别,也位于同一目录,但是如果您具有以下文件夹结构,例如:

/public/views/
-layout.pug
/main/
-index.pug
-about.pug

Then you must scale one directory up like extends ../layout . 然后,您必须像extends ../layout一样放大一个目录。 Hope this help someone starting in this template world. 希望这对从这个模板世界开始的人有所帮助。 Cheers ;) 干杯;)

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

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