Express Handlebars not rendering Sections

27 Views Asked by At

I am currently learning about templates in Node Express handlebars, but I can't seem to get them working. This is what my code looks like: index.js

const express = require('express')
const expressHandlebars = require('express-handlebars')

const app = express()

app.engine('handlebars', expressHandlebars.engine({
    defaultLayout: 'main',
    helpers: {
        section: function(name,options) {
            if(!this._sections) this._sections = {}
            this._sections[name] = options.fn(this)
            return null
        },
    },
}))
app.set('view engine', 'handlebars')

app.get('/section-test', (req,res) => res.render('section-test'))

main.handlebars this is my layout

<!DOCTYPE html>
<html>
    <head>
        <title>Meadowlark Travel</title>
        {{{_section.head}}}
    </head>
    <body>
        {{{body}}}
        {{{_section.scripts}}}
    </body>
</html>

This is my view looks like section-test.handlebars

{{#section 'head'}}
    <!-- We want google to ingore this page -->
    <meta name="robots" content="noindex">
{{/section}}

<h1>Test Page</h1>
<p>We're testing some script stuff</p>

{{#section 'scripts'}}
    <script>
        document.querySelector('body').insertAdjacentHTML('beforeEnd', '<small>(scripting Works!)</small>')
    </script>
{{/section}}

So I run node index.js it runs the server and I navigate to the endpoint http://localhost:(port)/section-test. I see the "Test Page, We're testing some script stuff" text but I dont see any of the other text that is suppose to be interpolated into the main layout. I'm not to sure what is going wrong anyone have an idea

0

There are 0 best solutions below