Is there a way to build a handlebars partial path with regex?

49 views Asked by At

I am building a small checklist app with express.js and handlebars.js. The app has a checklist that, on completion and submission, will route the user to a page with a small animation. The animation is in a partial file, called by that page.

What I would like to do is generate a random number that I can then use to build a string with, for my partial call. The string will end with a partial file name that ends with that number, and should be able to have any text preceeding that number. That way, I can designate which partials I want to make available at a given time, simply by appending a number to the end of the .hbs filename in the app.

I'm able to use the randomly generated number effectively, but I am having trouble including a regex expression in the string. I thought by using the following I would be able to achieve what I am looking for:

checklist.js

    router.get('/bedtime-checklist-complete', (req, res, next) => {

    let number = Math.round(Math.random() * 2);
    res.locals.selection = 'animations/' + /.*/ + number;

    //end helper
    res.render('checklists/bedtime-checklist-complete', {
        pageTitle: "Checklist complete!", 
        bodyClass: "checklist-complete"
    });
})

res.local.selection is the variable that will be passed to bedtime-checklist-complete.hbs, and it will be the path I'm trying to use with handlebars {{}} syntax and lookup function to call in the partial:

bedtime-checklist-complete.hbs

 <main>
        <h1>Great work!</h1>
        <div id="imgContainer">
            {{> (lookup . 'selection') }}
        </div>
    </main>

However, what is happening is that the selections variable I'm building in the express router and then passing to the handlebars partial is being passed litterally, so '//.*/' is in the path. Is there a way I can make the regex work with the partial call?

enter image description here

0

There are 0 answers