javascript - Handlebars -- script in precompiled template doesn't run -


i have precompiled handlebars template. having html sending values (like normal), decided wanted include script executed when inserted dom. however, haven't been able script run.

a lot of people seem have run across issue when handlebars template not precompiled, haven't seen many issues precompiled.

here's basic structure of .hbs file:

<div>     <h1>{{title}}</h1> </div> <script>     // please     alert('executing script'); </script> 

i'm injecting handlebars dom , part working (the html injected , variables getting set). script never executes, however.

any ideas?

as mentioned in comment wrote while ago, can't add script directly handlebars template.

you can create new html tag, dynamically treat code job want do. example:

<div data-handlebars-script="example-stack-overflow"></div> 

or

<example-stack-overflow /> 

(i prefer go more standard way - first one).

and after render template can use e.g. jquery.

$('div[data-handlebars-script="example-stack-overflow"').each(function() {    // stuff here }); 

edit

let me give comment on side. if need such thing, may indicate kind of flaws in design.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -