question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Including partials dynamically

See original GitHub issue

Hi,

I have been looking for a simple way to dynamically render partials based on a value in an {{#each ...}} loop.

For example:

{{#each modules}}
    {{> @exphbs.partials[ this.name ] }}
{{/each}}

This would allow me to load partials based on data rather than putting a lot of logic in the template.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ericfcommented, Apr 9, 2015

Looking at the unit tests the subexpression needs to be a helper. Something like this:

var app = express();
app.set('view engine', 'hbs');
app.engine('hbs', exphbs({
    helpers: {
        partial: function (name) {
            return name;
        }
    }
}));
{{#each modules}}
    {{> (partial this.name) }}
{{/each}}
1reaction
sidonaldsoncommented, Dec 14, 2017

If you’re using the handlebar-helpers library you can use a standard string function such as lowercase. It might not do anything but it returns a function and fixes this niggle.

{{> (lowercase this.url)}}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I load different partials dynamically using handlebars ...
First, pull in your dynamic partials before rendering, something like: ... So when you use a helper to include a partial then Handlebars ......
Read more >
How to best dynamically include partials in a view? - Laracasts
In my example, I'm looking to include partials based upon the property of an object. I could simply do a if-else, but that...
Read more >
Partials | Handlebars
Dynamic Partials. It's possible to dynamically select the partial to be executed by using sub expression syntax. template
Read more >
How to build a dynamic partial? - How To - Wappler Community
In my nodejs content page, I have 4 tables with same content inside. The only changes in this bootstrap table is that the...
Read more >
Handlebars: Dynamic Partial
Samples · The return value of helper foo will be partial name · When partial name is not exist it will cause runtime...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found