Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output a raw block? #73

Closed
phobia opened this issue Feb 19, 2014 · 11 comments
Closed

How to output a raw block? #73

phobia opened this issue Feb 19, 2014 · 11 comments

Comments

@phobia
Copy link

phobia commented Feb 19, 2014

Hi,

In a page I want to output a block of content without handlebars parsing anything inside it. Can it be done?

The case is that I want to embed a handlebars template for the client side javascript to use, but since some of the variables are named the same, they are parsed by handlebars through Assemble.

I'm imagining something like this:

{{#raw}}
<script type="text/x-handlebars-template">
    <li>{{title}}</li>
</script>
{{/raw}}
@jonschlinkert
Copy link
Member

@jonschlinkert
Copy link
Member

also, please reopen if you require something different

@phobia
Copy link
Author

phobia commented Feb 20, 2014

Thanks for the quick reply!

The downside of the mentioned techniques is that one couldn't reuse a template both server and client side.

I ended up creating a custom helper that solved my need: https://gist.github.com/phobia/9110443

Maybe something like that could be considered as part of the helper collection for Assemble?

@jonschlinkert
Copy link
Member

Makes sense, you're just caching the partial! I don't know I didn't think of that! lol.
Yea, if you want to create a PR for this over at https://github.com/assemble/handlebars-helpers, I'd be happy to merge - @doowb, okay with you?

@doowb
Copy link
Member

doowb commented Feb 20, 2014

@phobia I think that Handlebars will cache the compiled partial function after it's first use, so your gist might show JavaScript functions instead of just the raw partial.

Also, they added a way to show output "raw" templates in alpha. You still have to wrap it in some type of helper, but you could change your helper to be a simple pass-through...

Handlebars.registerHelper('raw', function(options) {
  return options.fn();
});

Then if someone wants it to get compiled and rendered, they use it normally...

{{raw}}
  <span>{{foo}}</span>
{{/raw}}

If they want it to be the raw template, the use 4 mustaches

{{{{#raw}}}}
<span>{{foo}}</span>
{{{{/raw}}}}

@Ramblurr
Copy link

Ramblurr commented Nov 5, 2014

Now that is many month later, is there an approved/official way to do this? I've upgraded assemble to use handlebars 2.0.0, but I'm not sure how to register the raw helper.

in my helpers.js file I've got:

module.exports.register = function (Handlebars, options)  {
    Handlebars.registerHelper('raw-helper', function(opts) {
        return opts.fn();
    });
};

But assemble is failing with:

Running "assemble:dev" (assemble) task
Assembling www/index.html ERROR
Warning: Parse error on line 35:
...lper}}}{{test}}{{{/raw-helper}}}
---------------------^
Expecting 'ID', 'DATA', got 'SEP' Use --force to continue.

@brunowego
Copy link

I get this error too. @Ramblurr

@Ramblurr
Copy link

@jonschlinkert or @phobia Could you please reopen this bug so it will get looked at?

@jonschlinkert
Copy link
Member

Did you copy @doowb's example exactly? If so, he has a typo. It should be:

{{{{#raw}}}}
<span>{{foo}}</span>
{{{{/raw}}}}

That said, this really isn't an assemble issue. Assemble just runs Handlebars and reports errors. This is handlebars syntax and it's something that can be solved with handlebars helpers (but even if it can't, it still wouldn't be an assemble issue since assemble doesn't do any kind of parsing. e.g. there is nothing assemble itself can do to make that work. it has to be done in the template engine: handlebars). I don't mean that we don't like to help, of course we do. Just trying to point you in the right direction.

@mootari
Copy link

mootari commented Jan 7, 2018

In case anyone else runs into this problem within markdown content, this would be the solution:

{{{{markdown}}}}{% body %}{{{{/markdown}}}}

The missing # is correct.

@landwire
Copy link

Hi there,
sorry to re-open this issue, but I think it's maybe fairly related.
Instead of trying to use raw (or markdown) like this:

{{{{raw}}}}
<span>{{foo}}</span>
{{{{/raw}}}}

I would like to output the contents of a partial (foo.hbs) in raw format:

{{{{raw}}}}
{{>foo}}
{{{{/raw}}}}

I tried this with markdown:

{{{{markdown}}}}
{{>foo}}
{{{{/markdown}}}}

and it outputs this: {{>foo}}
But I would like this: <h1>This is the {{title}}</h1>

foo.hbs
<h1>This is the {{title}}</h1>

Anybody could give me a hint on how to achieve this?
The gist here: https://gist.github.com/phobia/9110443 sometimes just returns functions as @doowb already stated, so it is no help to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants