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.

Feature: contentFor block helper

See original GitHub issue

Love the project. By far the easiest to get going so far for this node.js/express newbie.

The express-hbs project has a really neat feature I’d love to see added. I’m not well enough versed yet to create this and file a pull request. The contentFor helper is basically equiv to content placeholders in asp.net and very useful. Basically allows to adding content to a parent template/layout. Here’s the usage from express-hbs

layout.handlebars

{{{block "pageScripts"}}}

index.handlebars

{{#contentFor "pageScripts"}}
  CONTENT HERE
{{/contentFor}}

Is this something we can get added, or would it be possible to get a helper function we can add as middleware?

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
smencercommented, Dec 6, 2013

Just thought I’d chime in to say I was able to get the sample code above working in my application with a small modification:

    helpers: {
      block: function(name){
        var blocks = this._blocks;
            content = blocks && blocks[name];
        return content ? content.join('\n') : null;
      },
      contentFor: function(name, options){
        var blocks = this._blocks || (this._blocks = {});
            block = blocks[name] || (blocks[name] = []); //Changed this to [] instead of {}
        block.push(options.fn(this));
      }
    }

I’m now able to define a {{{block “pageScripts”}}} in my layout page and fill it with content from my views inside of:

{{#contentFor "pageScripts"}}
<script>
 // my page specific code here
</script>
{{/contentFor}}

Thanks for the sample code to get me jump started!

5reactions
ericfcommented, Nov 26, 2014

Thanks! Glad this project is useful to you 😃

This feature is definitely something that’s perfect for helpers. Here’s one way you could go about doing this:

var express = require('express'),
    exphbs  = require('express3-handlebars'),

    app = express(),
    hbs;

hbs = exphbs.create({
    helpers: {
        block: function (name) {
            var blocks  = this._blocks,
                content = blocks && blocks[name];

            return content ? content.join('\n') : null;
        },

        contentFor: function (name, options) {
            var blocks = this._blocks || (this._blocks = {}),
                block  = blocks[name] || (blocks[name] = []);

            block.push(options.fn(this));
        }
    }
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

// ...

Let me know if this helps (or even works, I didn’t test it.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

block & contentFor - Ghost Handlebars Theme Helpers
{{/contentFor}} helper is used to access and populate the block definitions within the template that's being inherited. The inherited template is referenced ...
Read more >
express-hbs - npm
Express handlebars template engine complete with multiple layouts, partials and blocks.. Latest version: 2.4.0, last published: a year ago.
Read more >
ruby on rails - Rendering a content_for block in a helper
I'm trying to render the result of a content_for block through a helper. I have a template (HAML) and a layout as follows:...
Read more >
Path Helpers - Buffalo – Rapid Web Development in Go
The contentFor helper takes a block of HTML and holds on to it using the given name. This block can then be used...
Read more >
Helper Methods - Middleman
Template helpers are methods which can be used in your dynamic templates to ... take a block, allowing you to provide more complex...
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