Code Monkey home page Code Monkey logo

Comments (10)

smencer avatar smencer commented on July 29, 2024 9

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!

from express-handlebars.

ericf avatar ericf commented on July 29, 2024 4

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.)

from express-handlebars.

shellscape avatar shellscape commented on July 29, 2024

That's awesome. I'll play with this some tomorrow. Thanks!

from express-handlebars.

ericf avatar ericf commented on July 29, 2024

@shellscape Did this end up working out for you?

from express-handlebars.

shellscape avatar shellscape commented on July 29, 2024

Haven't yet gotten to test that, but its in the pipe. Thanks very much.
On Mar 27, 2013 1:57 PM, "Eric Ferraiuolo" [email protected] wrote:

@shellscape https://github.com/shellscape Did this end up working out
for you?


Reply to this email directly or view it on GitHubhttps://github.com//issues/15#issuecomment-15540748
.

from express-handlebars.

ericf avatar ericf commented on July 29, 2024

Closing this…

from express-handlebars.

MacgyverMartins avatar MacgyverMartins commented on July 29, 2024

It doesn't work for me. It just returns a string inside the HTML and nothing happens

contact.hbs

{{#contentFor "pageScripts"}}
  <script src="js/pace.min.js"></script>
  <script src="js/wow.min.js"></script>
{{/contentFor}}

main.hbs

{{block "pageScripts"}}

from express-handlebars.

panlw avatar panlw commented on July 29, 2024

Great!

from express-handlebars.

ignacionr avatar ignacionr commented on July 29, 2024

This is working really well. Cheers!!

from express-handlebars.

rajitha-bandara avatar rajitha-bandara commented on July 29, 2024

To properly run js, instead of displaying it,
I needed to change the notation from
{{block "pageScripts"}} to {{{block "pageScripts"}}}

from express-handlebars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.