标签: ejs

css 不使用express .ejs nodejs 加载属性

各位晚上好,

我将不胜感激任何帮助。

我得到了这两条路线:

app.get('/', function(req,res){

        res.render('index');
});

app.get('/registration/add', function(req,res){

        res.render('clientRegistration');
});
Run Code Online (Sandbox Code Playgroud)

这是服务器的一部分:

app.use( express.static(path.join( __dirname, './client/static/')));
app.use(body_parser.urlencoded());
app.set( "views", path.join(__dirname, "./client/views/"));
app.set( "view engine", 'ejs');
Run Code Online (Sandbox Code Playgroud)

目录是:

MyApp/
    client/
        static/
            registration.css
            styles.css
        views/
            index.ejs
            clientRegistration.ejs
Run Code Online (Sandbox Code Playgroud)

在我的 index.ejs 上,我有: link rel="stylesheet" type="text/css" href="styles.css" 可以完美加载 .css 文件。但是,当我单击index.ejs上的链接:a href="/registration/add">时,它会将我带到clientRegistration.ejs页面,但即使我有:link rel="stylesheet",但registration.css也不会加载类型=“文本/css”href=“registration.css”

我究竟做错了什么?

谢谢你的时间。

css ejs node.js express

2
推荐指数
1
解决办法
1525
查看次数

语法错误:意外的标记;编译ejs时

我正在尝试在我的 Express 应用程序中使用此谷歌地图代码,但它给了我一个语法错误。我已经过滤了这个函数的错误:

  <% var map; %>
  <% function initMap() { %>
  <%  map = new google.maps.Map(document.getElementById('map'), { %>
  <%    center: {lat: -34.397, lng: 150.644}, %>
  <%    zoom: 8 %>
  <%  }); %>
  <% }; %>
Run Code Online (Sandbox Code Playgroud)

错误消息并没有多大帮助:

SyntaxError: Unexpected token ; in C:\proyectos\google-location-api\views\resultados.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
    at Object.compile (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:524:12)
    at Object.compile (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:338:16)
    at handleCache (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:181:18)
    at tryHandleCache (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:203:14)
    at View.exports.renderFile [as engine] (C:\proyectos\google-location-api\node_modules\ejs\lib\ejs.js:412:10)
    at View.render (C:\proyectos\google-location-api\node_modules\express\lib\view.js:128:8) …
Run Code Online (Sandbox Code Playgroud)

ejs node.js express

2
推荐指数
1
解决办法
1万
查看次数

如何使用 Node.js 后端访问 EJS 模板中的会话数据?

我根据用户的角色创建了一个动态菜单栏。在服务器端,当用户登录时,我将他们所有允许的页面存储在他们的会话中。

现在,我有 15 个模板。menubar.html我在每一页上都包含了文件。我想使用会话数据,而menubar.html不必每次都显式传递它。

我尝试过以下代码:

app.get('/home',function(req,res)){
  res.render('home',{menu : req.session.menubar})
}
app.get('/contact',function(req,res)){
  res.render('contact',{menu : req.session.menubar})
}    
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我需要在每个页面上传递会话数据。如果我有 50 个 html 模板,那么在每个路由中传递会话的效率并不高。

处理这个问题的最佳方法是什么?

ejs node.js express node-modules

2
推荐指数
1
解决办法
7437
查看次数

如何将对象从 EJS 传递到 Node.js 路由器 - Javascript、EJS、Node.js、MySQL

我正在循环遍历 MySQL 表中的记录,将它们显示在 EJS 文件中,然后单击其中一个,路由到页面以编辑记录。但是,我无法将单个对象传递回我的routes.js 文件以将其路由到编辑页面。我循环浏览所有记录并打印它们没有任何问题,所以我知道数据就在那里。这是 EJS 文件的一部分,我在其中输出所有记录以供用户选择:

<body>
<div class="container">

<div class="col-sm-6 col-sm-offset-3">

    <h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
    <p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>

    <% for (var i=0; i < Agreements.length; i++) { %>
        <a href="/editAgreement" style="text-decoration: none; color: #333333">
            <div class="well" align="center">

                <h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
                <p><%= Agreements[i].roommate1%></p>
                <p><%= Agreements[i].roommate2%></p>
                <% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
            </div> …
Run Code Online (Sandbox Code Playgroud)

javascript mysql ejs node.js express

2
推荐指数
1
解决办法
4997
查看次数

ejs 模板中的 if-else 简写?

下面是我正在渲染的 EJS 文件中的代码,但我无法找出 if 语句简写的正确语法。

这里的“cchoice”是我在渲染时传递的对象,如果我删除 if 语句它就可以工作,因此获取它的值没有问题。

<h1>Welcome,
    <%= cchoice %>
</h1>
<% cchoice =='dog' ? <p>good choice</p> : <p>cool!</p> %>
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

SyntaxError: 编译 ejs 时 /views/new.ejs 中出现意外的标记 <

为上述 if 语句的简写提出正确的语法建议。

javascript if-statement ejs conditional-statements express

2
推荐指数
1
解决办法
2782
查看次数

使用 EJS 和 HTML 向已添加类的元素添加其他类?

我目前正在尝试使用 ejs 为导航栏开发部分模板文件,但是我正在努力向已添加类的元素添加其他类。

我尝试了两种添加附加类的方法,这是第一种:

<li class='navbar-item'<% if (pageTitle === 'Home') { %> class="active" <% } %>> <a href="/home">Home</a></li>
Run Code Online (Sandbox Code Playgroud)

然而这不起作用,所以我决定尝试将 EJS 嵌入到类属性中,如下所示

<li class="nav-item <% if (pageTitle === 'Home') { %> active <% } %>"> 
Run Code Online (Sandbox Code Playgroud)

这也不起作用,我确保 pageTitle 被正确提供,因为它在网页中的其他地方使用并且工作正常。

我不确定下一阶段是什么,我考虑过使用额外的 JS 文件,但这似乎适得其反,因为我已经能够将 JS 嵌入到 HTML 中。

感谢任何帮助,提前致谢。

html javascript css ejs node.js

2
推荐指数
1
解决办法
2481
查看次数

使用 EJS 在列表中显示数组

我正在制作一个简单的待办事项列表来学习 Node.js,并使用 EJS 进行模板化。我正在使用 for 循环来循环访问我设置的数组,以显示列表中的每个项目,并添加一个表单供用户添加到列表中。我的问题是,现在我已经创建了这个数组,但我尝试渲染它的地方什么也没有出现。显示数组时是否缺少某些内容?

编辑:我应该解释一下,我的特定代码(对于一个类)专门使用 for 循环而不是 forEach() 方法。我的问题也应该反映出我看不到我的代码和 stackoverflow 上其他一些代码之间的区别,因为这是一个拼写错误。

列表.ejs

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>To Do List</title>
</head>
<body>
<h1><%= whichDay %></h1>

<ul>
    <% for (var i = 0; i < newListItems; i++) { %>
        <li> <%= newListItems[i] %> </li>
    <% } %>
</ul>

<form class="new-item" action="/" method="POST">
    <input type="text" class="" name="newItem" placeholder="Add to list">
    <button type="submit" name="button">Add</button>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

应用程序.js

const bodyParser = require(`body-parser`); …
Run Code Online (Sandbox Code Playgroud)

javascript arrays ejs node.js

2
推荐指数
1
解决办法
1万
查看次数

SyntaxError:在nodejs中使用ejs时出现意外的标识符

我正在 YouTube 上关注 The Net Nijna 的教程。我到达了教程 27,在 ejs 中使用部分。一切正常,直到我添加 <% includepartials/nav.js %>,一旦我添加此代码,我就会收到:

SyntaxError:编译 ejs 时, (文件位置) testapp\views\profile.ejs中出现意外标识符

如果上述错误没有帮助,您可能想尝试 EJS-Lint: https://github.com/RyanZim/EJS-Lint 或者,如果您打算创建异步函数,请async: true作为选项传递。在新功能()......等等等等......

如果我删除它,我的 ejs 一切正常。

  <body>
    <% include partials/nav.ejs %>

    <h1>Welcome to the profile of <%= person %> !</h1>
    <p><strong> Age: <%= data.age %></strong></p>

    <p><strong> Job: <%= data.job %></strong></p>
    <p><strong> Pet: <%= data.pet %></strong></p>
    <h2>Hobbies</h2>
    <ul>
      <% data.hobbies.forEach(function(item){ %>
        <li><%= item %></li>
      <%});%>
    </ul>
  </body>
Run Code Online (Sandbox Code Playgroud)

你能帮助学生吗?万分感谢!

ejs node.js

2
推荐指数
1
解决办法
1674
查看次数

Express.js 应用程序错误:无法读取未定义的属性“transfer-encoding”

我正在使用ExpressEJS和 MongoDB开发一个博客应用程序(单击链接查看GitHub存储库)。

我正在尝试引入添加帖子图像功能。作为 Express 的新手,我对遇到的问题感到困惑。

添加帖子表单:

<form action="/dashboard/post/add" method="POST" enctype="multipart/form-data" class="mb-0">
  <div class="form-group">
    <input type="text" class="form-control" name="title" value="<%= typeof form!='undefined' ? form.titleholder : '' %>" placeholder="Title" />
  </div>
  <div class="form-group">
    <input type="text" class="form-control" name="excerpt" value="<%= typeof form!='undefined' ? form.excerptholder : '' %>" placeholder="Excerpt" />
  </div>
  <div class="form-group">
    <textarea rows="5" class="form-control" name="body" placeholder="Full text"><%= typeof form!='undefined' ? form.bodyholder : '' %></textarea>
  </div>
  <label for="postimage">Upload an image</label>
  <div class="form-group">
    <input type="file" name="postimage" id="postimage" …
Run Code Online (Sandbox Code Playgroud)

ejs node.js express

2
推荐指数
1
解决办法
5661
查看次数

怎么才能只显示100个字符的帖子?

我目前正在创建一个博客网站,在主页上,我只想显示该段落的 100 个字符。之后我只想显示 ...(3 个点)如何使用 javascript 或 css 来做到这一点? 在此输入图像描述

我希望它看起来像: 在此处输入图像描述

html javascript css ejs node.js

2
推荐指数
1
解决办法
1万
查看次数