Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use JavaScript inside handlebars.js template

The description says it all. How to put a JavaScript script inside handlebars template.

I want to make a dynamic Paypal button for my website.

      <script type="text/x-mustache-template" id="product-item-thumbnail-template">
      <h2>{{title}}</h2>
      <p>{{message}}</p>
      <p><a class="btn" href="#">View details &raquo;</a></p>
      <p><script src="resources/js-frameworks/[email protected]"
            data-button="buynow"
            data-name="My product"
            data-amount="1.00"></script></p>
</script>

But this produces an error because of the tag. it closes the template script and not the paypal script

Thanks

like image 361
Gleeb Avatar asked Jun 28 '13 16:06

Gleeb


People also ask

Can you put JavaScript in Handlebars?

The Handlebars.js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces. These expressions are placeholders for content to be inserted by our Handlebars.

Is Handlebars a template language?

Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.

Is Handlebars a template engine?

Handlebars: What it is and Why to Use itHandlebars is a logic-less templating engine that dynamically generates your HTML page. It's an extension of Mustache with a few additional features.

How Handlebars js is different from Mustache js?

Mustache is missing helpers and the more advanced blocks because it strives to be logicless. Handlebars' custom helpers can be very useful, but often end up introducing logic into your templates. Mustache has many different compilers (JavaScript, Ruby, Python, C, etc.).


1 Answers

Try breaking up the "/script" part:

<script ...><{{!}}/script>

That technique is similar to older hacks like

<script>document.write("<script>...<\/script>");</script>
like image 162
kay Avatar answered Oct 08 '22 04:10

kay