I am trying to convert this ejs code to handlebars. I am taking a tutorial so it's giving me a bit of problems. I would be glad to get any help
<% if(typeof errors!= 'undefined') { %>
<% errors.forEach(function(error){ %>
<p> <%= error.msg %></p>
<% }) %>
<% } %>
This is my conversion:
{{#if typeof errors !== "undefined"}}
{{#each error}}
<p>{{error.msg}}</p>
{{/each}}
{{/if}}
But I keep getting this error:
Error: Parse error on line 1:
{{#if typeof errors !== "undefined"}}
--------------------^
Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'EQUALS', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', 'SEP', got 'INVALID'
at Parser.parseError (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:267:19)
at Parser.parse (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/parser.js:336:30)
at parseWithoutProcessing (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:46:33)
at HandlebarsEnvironment.parse (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/base.js:52:13)
at compileInput (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:508:19)
at ret (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:517:18)
at Object.invokePartial (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/runtime.js:334:12)
at Object.invokePartialWrapper [as invokePartial] (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/runtime.js:84:39)
at Object.eval [as main] (eval at createFunctionContext (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:262:23), <anonymous>:10:31)
at main (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/runtime.js:208:32)
at ret (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/runtime.js:212:12)
at ret (/home/bunny/Tales/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:519:21)
at ExpressHandlebars._renderTemplate (/home/bunny/Tales/node_modules/express-handlebars/lib/express-handlebars.js:250:9)
at ExpressHandlebars.<anonymous> (/home/bunny/Tales/node_modules/express-handlebars/lib/express-handlebars.js:173:15)
Also, I want to add this ejs partial file to my handlebars and this is what I did. I don't know how correct I am:
Ejs:
<%- include ('./partials/messages') %>
Handlebars:
{{> _messages}}
Your handlebars syntax doesn't look right, your conversion should be:
{{#if errors }}
{{#each errors}}
<p>{{ this.msg }}</p>
{{/each}}
{{/if}}
{{#if errors }}
checks if error
is undefined
or []
(https://handlebarsjs.com/guide/builtin-helpers.html#if)
Also, this
is used to reference the element being iterated over (https://handlebarsjs.com/guide/builtin-helpers.html#each).