Search code examples
node.jshandlebars.jsexpress-handlebars

Using nodejs and handlebars


Hi everybody I'm kind new in nodejs problems I hope o can found what I need as simple as possible So my base problem is how can I use multiple handlebars files in one handlebar page as sections Like to have a page for the headear and another for the body and others for footer and all that just by using nodejs and handlebars ... And thanks after all ^^


Solution

  • You can define the pages except main page as partials. Then you the files in your main file in the form of {{> filename}}

    for eg:

    views/
        main.handelbars
        partials/
            -header.handelbars
            -body.handelbars
            -footer.handelbars
    

    main.handlebars

    <html>
    <title>xxxxxx</title>
    <head>
    .....
    </head>
    {{> header}}
    {{> body}}
    {{> footer}}
    </html>
    

    header.handlebars

    <header>
    ........
    </header>
    

    body.handlebars

    <div>
    ........
    </div>
    

    footer.handlebars

    <footer>
    ........
    </footer>
    

    Dont't forget to add the view engine and mention the public directory to views as well as the partials directory.