A
A
ARTESS7772021-05-23 23:11:40
Node.js
ARTESS777, 2021-05-23 23:11:40

Why don't handlebars helpers work?

Hello connoisseurs. It turns out I scored a complete hodgepodge: there are layout hbs, partials hbs. Here I need the helper to output a string, but it does not return anything. Here absolutely nothing displays, no matter how I dance with a tambourine. It's like he doesn't exist. Now I will show under where hbs is involved:

//Addind modules

// Express
const express    = require("express");
// Шаблонизатор
const exphbs     = require("express-handlebars");
const hbs        = require("hbs");
// бд
const mongoose   = require("mongoose");
// сессии
const session    = require("express-session");
// хранилище сессий
const mongoSrote = require("connect-mongo");

// локальное хранилище
const LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');

//connection routs
const homeRouter = require("./routs/homeRout.js")

// создаём приложене
const app = express();


// регистрация хелпера
hbs.registerHelper("getNav", function(){
  let nav = '<li><a href="/">Мои бич</a></li>';
  nav += '<li><a href="/create">Добавить новую</a></li>';
  nav += '<li><a href="/account">Мой аккаунт</a></li>';
  if(localStorage.getItem("userLogin")){
    nav += '<li><a href="/account">Мой аккаунт</a></li>';
  }
  else{
    nav += '<li><a href="/create">Добавить новую</a></li>';
  }
  return new hbs.SafeSrting("<p>a</p>");
});

hbs.registerHelper("lol", function(){
  return "ew";
});

// шаблонизатор
app.engine("hbs", exphbs({
  defaultLayout: "main",
  extname: "hbs",
}));
app.set("view engine", "hbs");


The helper should work here:
<nav>
  <ul>
      <li>{{getNav}}</li> 
      <li><a href="/logout">Выйти</a></li>
      <li><a href="/register">Регистрация</a></li>
      <li><a href="/login">Войти</a></li>
  </ul>
</nav>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question