简体   繁体   中英

How I can append js into a express handlebars template using partial?

In my project I use the following handlebars template (under views/layouts/main.handlebars ):

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{title}}</title>
    {{{css}}}
    {{{js}}}
</head>
<body>

    <header>
      <!-- some content here -->
    </header>
    <main>
        {{{body}}}
    </main>
    <footer>
        I am footer
    </footer>
</body>
</html>

And for my home page I made a simple content named ./views/home.handlebars

<h1>Hello</h1>

In order to render the template I use express handlebars:

const express = require('express');
const path = require('path');
const express_handlebars = require('express-handlebars');

const app = express();

const hbs = express_handlebars.create({
    // Specify helpers which are only registered on this instance.
    defaultLayout: 'main'
});

app.engine('handlebars', hbs.engine); 
app.set('view engine', 'handlebars');
app.set('views',path.join(__dirname,'/../../views'));

app.get('/', (req, res, next) => {
    res.render('home');
});

app.listen(3000);

What I want is to place some javascript once I visit http://0.0.0.0:3000 for example:

<script>alert("Hello");</script>

Where I want to be appended is in {{{js}}} partial. Is there a way to do it?

An approach of mine is to make the js section as an array and place a base html5 tag, specifying where each page will find its js:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{title}}</title>
    {{{css}}}
    {{#each js}}
        <script src="{{this}}"></script>
    {{/each}}
</head>
<body>

    <header>
      <!-- some content here -->
    </header>
    <main>
        {{{body}}}
    </main>
    <footer>
        I am footer
    </footer>
</body>
</html>

For my application's js I use separate files per page and I do not embed it inside my homepage into a folder named static :

const express = require('express');
const path = require('path');
const express_handlebars = require('express-handlebars');

const app = express();

const hbs = express_handlebars.create({
    // Specify helpers which are only registered on this instance.
    defaultLayout: 'main'
});

app.engine('handlebars', hbs.engine); 
app.set('view engine', 'handlebars');
app.set('views',path.join(__dirname,'/../../views'));

app.use('/static',express.static(path.join(__dirname,'/../static')));

app.get('/', (req, res, next) => {
    res.render('home',{
        title:'Homepage',
        js: [
            '/static/js/home.js'
        ],
        baseUrl: `${req.protocol}://${req.get('host')}`
    });
});

app.listen(3000);

Then upon static folder I place the necessary js files that will run upon the browser. I found it as an easy-peasy solution and keep client-side js into its own folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM