把手 - 期待'OPEN_INVERSE_CHAIN','INVERSE','OPEN_ENDBLOCK',得到'EOF'

Ann*_*lee 10 javascript node.js express handlebars.js

我正在尝试把手,我正在使用以下简单的模板:

<html>

<head></head>

<body>

    <h1>{{title}}</h1>
    <p>{{body}}</p>

    {{#each list}}
    <ul>
        <li>{{@index}}. {{this}}</li>
    </ul>
    </br>
    {{{htmlTag}}} 

    {{#if user.admin}}
    <button class="launch">Launch Spacecraft</button> {{else}}
    <button class="login"> Log in</button> 
    {{/if}} 
    {{!-- This is a comment --}}

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

app.js看起来如下:

require('dotenv').config()
const express = require('express')
const logger= require('morgan')
const path = require('path')

const app = express()

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'hbs')
app.use(logger(process.env.LOG_ENV))

app.get('/', (req, res) => {
    const titel = "This is a titel"
    const body = "Lorem ipsum dolor sit amen ..."
    const list = ['apple', 'banana', 'vegetable']
    const htmlTag = '<h2>This is an unescaped Heading</h2>'
    const user = {
    admin: true
  }
    res.render('index',{titel, body, list, htmlTag, user})
})


// Start Server
const port = process.env.APP_PORT || 8080
const host = process.env.APP_URL || '0.0.0.0'

app.listen(port, host, () => {
    console.log(`Listening on ${host}:${port}/`)
})

module.exports = app
Run Code Online (Sandbox Code Playgroud)

当我呈现页面时,我收到以下错误:

错误:/home/ubuntu/workspace/src/views/index.hbs:第20行解析错误:...}} ------------------- ^期待' OPEN_INVERSE_CHAIN','INVERSE','OPEN_ENDBLOCK',在Object的Object.parseError(/home/ubuntu/workspace/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:267:19)获得'EOF'.在HandlebarsEnvironment.parse解析(/home/ubuntu/workspace/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:336:30)(/ home/ubuntu/workspace/node_modules/handlebars/dist/cjs/handlebars /compiler/base.js:46:43)在ret(/ home/ubuntu/workspace)的compileInput(/home/ubuntu/workspace/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:514:19) /node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:523:18)athome/ubuntu/workspace/node_modules/hbs/lib/hbs.js:63:19 at tryToString(fs.js:456) :3)在FSReqWrap.readFileAfterClose [as oncomplete](fs.js:443:12)

在我的模板中有什么建议我做错了吗?

感谢您的回复!

wsc*_*rge 23

错误状态:

期待(......),得到了EOF

哪里:

  1. (...)是它可能期望的
  2. EOF是文件结束

你缺少的是关闭{{/each}}:

{{#each list}}
<ul>
    <li>{{@index}}. {{this}}</li>
</ul>
</br>
{{{htmlTag}}} {{#if user.admin}}
<button class="launch">Launch Spacecraft</button> {{else}}
<button class="login"> Log in</button> {{/if}} {{!-- This is a comment --}}
{{/each}} <!-- HERE -->
Run Code Online (Sandbox Code Playgroud)


小智 5

这始终是不收任何的结果{{each}},{{if}}或任何其他控制研究报表,但它不能被未闭合的HTML标签一样becaused <div>,<p>或任何其他HTML标签.