Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

node.js - ejs 'partial is not defined'

Okay I have a mostly static homepage but I wanted to have partial views that for navigation, footer ect. I'm using ejs and it looks like this:

my controller: home.js

// Dependencies
var express = require('express');


    module.exports = {
        get: function(req, res) {
            app.set('view engine', 'ejs');  
            var model = {
            layout:'home',
                    };


            res.render('home');


        }
    };

My views directory has nav, home and footer all .ejs

Then the actual html file stripped of text would look as following.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<title>Tom Jones</title>

<!-- CSS -->
<link rel="stylesheet" href="/css/home.css" type="text/css" media="screen" >

</head>
<body>

<%- partial('nav') %>

<!--content part -->  
<div id="showcontainer">
        <section>

        </section>
</div>

<div id="maincontainer">
        <section>

        </section>
</div>

</body>
</html>

The Problem When ever I test it out I run into the error partial is not defined. I tried requiring ejs but no success.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As @Pickels said, Partial was removed in 3.x. However, the most recent version of EJS provides a mechanism for including "partials", called "include":

https://github.com/visionmedia/ejs#includes

Includes are relative to the template with the include statement, for example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <% include user/show %>. The included file(s) are literally included into the template, no IO is performed after compilation, thus local variables are available to these included templates.

The following will work as a replacement for your old partial() function. You'll need to make tweaks elsewhere to support Express 3.x completely, but for the most part this seems to work well (better actually - less code and more performant).

<% include nav.ejs %> <!-- replaces your old <%- partial('nav') %> -->

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...