Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render HTML Tag attribute using mustache

In my mustache template I do have something like:

<div {{attr}}="{{attrVal}}"></div>

Rendering this using

Mustache.render(template, {attr : 'data-test', attrVal : 'test'})

does produce

<div ="test"></div>

I expect to get something like

<div data-test="test"></div>

Isn't it possible to render attribute name inside of a tag using Mustache?

UPDATE

I figured out the problem. I define my HTML Mustache Templates inside custom <template> tags in my document. For example:

<template id='myTemplate'>
    <div {{dataAttr}}="{{dataAttrValue}}"></div>
</template>

When getting the template using document.querySelector("#myTemplate").innerHTML the browser does convert the {{dataAttr}} to {{dataattr}} because attributes are case insensitiv. So calling

Mustache.render(
    document.querySelector("#myTemplate").innerHTML, 
    { dataAttr : 'data-attr', dataAttrValue : 'test'}
);

Results in

<div ="test"></div>
like image 754
phwa4563 Avatar asked Feb 08 '16 13:02

phwa4563


People also ask

How do I render a mustache in HTML?

render = function (template, view, partials) { return this. compile(template)(view, partials); }; This is the most basic form of templating with mustache. Let's see the other methods available for creating more organized code.

What does Mustache render do?

Mustache is a simple web template system. It is available for many programming languages including JavaScript and Java. Mustache is described as a logic-less template engine because it does not have any explicit control flow statements, such as if and else conditionals or for loops.

What is mustache in HTML?

mustache. js is a zero-dependency implementation of the mustache template system in JavaScript. Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object.


3 Answers

Hope this code snippet will help you..

var template = document.querySelector("#template").innerHTML;
//Mustache.parse(template); // optional, speeds up future uses
var rendered = Mustache.render(template, {
  attr: "data-test",
  attrVal: "test"
});
document.querySelector("#target").innerHTML = rendered;
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.js"></script>
<body>
  <div id="target">Loading...</div>
  <template id="template" >
    <textarea style="width:300px">
      <div {{attr}}="{{attrVal}}"></div>
    </textarea>
  </template>
  
</body>
like image 138
Prabhakaran S Avatar answered Oct 23 '22 09:10

Prabhakaran S


I head the same problem Try the single [']:

<template id='myTemplate'>
    <div {{dataAttr}}='{{dataAttrValue}}'></div>
</template>

.....

like image 26
Ronald Wesselink Avatar answered Oct 23 '22 07:10

Ronald Wesselink


You can also try using to_html method for the expected output.

const HTML = Mustache.to_html(template, {attr : 'data-test', attrVal : 'test'});

document.getElementById("myTemplate").innerHTML = HTML;

const template = `
	<div {{attr}}="{{attrVal}}">
	</div>
`

const HTML = Mustache.to_html(template, {attr : 'data-test', attrVal : 'test'});

document.getElementById("myTemplate").innerHTML = HTML;

console.log(document.getElementById("myTemplate").innerHTML);
<html>
	<head>
	</head>
	<body>
		<template id='myTemplate'>
    </template>
    <div id="display-output"></div>
		<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.1.0/mustache.min.js"></script>
	</body>
</html>
like image 45
Ritu Avatar answered Oct 23 '22 09:10

Ritu