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

Including a <script> tag in a template? #531

Closed
julien51 opened this issue May 20, 2013 · 17 comments
Closed

Including a <script> tag in a template? #531

julien51 opened this issue May 20, 2013 · 17 comments
Labels

Comments

@julien51
Copy link

We're trying to include a couple gists in our content, but obviously adding <script src="https://gist.github.com/4332573.js"/></script> in a handlebar template breaks it.

Is there a way to "escape" the script tags?

@jorilallo
Copy link

You need to add them dynamically, i.e. https://gist.github.com/jeremiahlee/1748966

@julien51
Copy link
Author

Meh. That's quite ugly, but I guess if that's the only way, I'll use that =( There must be something smarter, please @wycats tell me there is something smarter! Thanks @jorde :)

@chrislondon
Copy link

I'm new here but I would think that precompiling your templates could solve the issue!

@kpdecker
Copy link
Collaborator

I'm not sure what is breaking for you but on the escaping side,I think this all depends on how the templates are being defined.

I would recommend the dynamic approach as script elements injected via innerHTML have different behaviors across different browsers, with some browsers, particularly older versions of IE, silently dropping content. Zepto attempts to work around this: https://github.com/madrobby/zepto/blob/master/src/zepto.js#L759-L763 but I would still tread carefully when attempting to do things like this.

@julien51
Copy link
Author

@kpdecker The problem is pretty simple. We use <script type="text/x-handlebars">...</script>to define our templates. Adding a <script src="https://gist.github.com/4332573.js"/></script> in the content will confuse handlebar because it will assume the end of the template is at the end of the gist... not where it actually ends.

I'll try the compile option, if that's the only option, but that seems a bit like an overkill:/

@kpdecker
Copy link
Collaborator

Precompiling is generally recommended as it can provide performance benefits (at the expense of requiring a build step). This is a total hack but you might be able to do something like:

</sc{{undefined}}ipt>

To work around the HTML parser's behavior here, but that feels like a total hack.

@kpdecker
Copy link
Collaborator

On the handlebars language side we should look how how to escape content generically.

@julien51
Copy link
Author

So, I eventually went with templates compilation... but that's still nto good, as it looks like, even though the <script> elements are included, they're not "executed" :/
Any idea?

@kpdecker
Copy link
Collaborator

If the script elements are in the DOM on final render but not executed then you may very well have to perform manual loading which is outside of the scope of handlebars. This is "here be dragons" from my experience on the subject (granted this was years ago)

@emirotin
Copy link

emirotin commented Jun 7, 2013

I though this could be solvable by a helper of such kind: http://codepen.io/emirotin/pen/rksCx
But for some reason the gist doesn't render

@kpdecker
Copy link
Collaborator

kpdecker commented Jul 5, 2014

After thinking about this again, I think that this is a relatively uncommon use case. Rather than creating an additional language construct for this, I've added documentation to the FAQ section highlighting the ability to use a comment to break up this construct when used in an inline template and urging users to use precompiled templates when possible.

https://github.com/wycats/handlebars.js/blob/master/FAQ.md

@kpdecker kpdecker closed this as completed Jul 5, 2014
@SeloSlav
Copy link

SeloSlav commented Feb 1, 2016

Can't you just escape the scripts like this? Probably not very secure though...

`

<script src="script1.js"><{{!}}/script> ` ` <script src="script2.js"><{{!}}/script> `

@letsrock85
Copy link

santafebound You are Genious!

@nknapp
Copy link
Collaborator

nknapp commented Jul 21, 2017

@letsrock85 may I recommend, that you precompile your templates? You avoid such problems altogehter and get a better performance.

@nevercast
Copy link

@nknapp Just to clarify, precompiling templates does not fix this. The script tag is embedded in the DOM, but it is not executed.

@nknapp
Copy link
Collaborator

nknapp commented Jul 7, 2018

@nevercast Handlebars just computes the string that contains the script.

What you do with the string is up to you. You can insert it into the DOM, but that does not execute the scripts. That is correct.

For solutions, see https://stackoverflow.com/questions/4619668/executing-script-inside-div-retrieved-by-ajax

@nevercast
Copy link

Thanks @nknapp, I ended up defining my own script type. <script type="text/x-after-hashchange"> and invoking those after handlebars had done its work. Which I believe is similar to the solutions you linked.

Ma27 added a commit to Ma27/community that referenced this issue Aug 1, 2019
When having e.g. a markdown-based section in a document where you
reference another document, a HTML-link is generated. However when
previewing changes, the raw HTML is shown.

With setting `html: true` in the configuration for `markdownit`[1],
HTML is detected and properly parsed (Also markup tags such as `<h1>`
are rendered properly just like when using `blackfriday` to request a rendered
section).

Regarding probably harmful side-effects: setting e.g. `<style>* { display:
none !important; }</style>` causes a white page with `markdownit` as
well as with `blackfriday`. `<script>` tags aren't affected since
`handlebars` mostly breaks with `<script>` tags within variables that
are substituted using `{{{var}}}` into the DOM[3].

Please note that I didn't commit the modifications in `embed/bindata.go`
as it seemed to me after looking at the history that those updates are
only done when preparing a release.

[1] https://github.com/markdown-it/markdown-it/blob/1ad3aec2041cd2defa7e299543cc1e42184b680d/lib/presets/default.js#L6-L9
[2] https://github.com/documize/blackfriday/blob/master/markdown.go#L105-L146
[3] handlebars-lang/handlebars.js#531
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants