Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html inside Template literals?

I explain my scenario, the users enter content through a WYSIWYG editor that is persisted in the database, in another place of the application this content and another one is obtained through an ajax request, here I use template literals to build a html structure with the data that is finally inserted into the view using innerHTML in a destination DOM element.

I require that the content that was added through the WYSIWYG editor be displayed as html, with lodash function _.unescape () I get the free content of html special characters, but this is not shown as html but as a html string

I share the general idea of ​​the implementation:

Template literal

`...
<div>${getHTML(dataset.message)}</div>
...`

Javascript

function getHTML(message) {
    const html = _.unescape(message).replace('\\', '');
    const parser = new DOMParser();
    const dom = parser.parseFromString(html, 'text/html')

    return dom;
}

Output

[object HTMLDocument]

If instead of the dom variable I return the html variable in the view I get for example <p>some content <strong>goes here</strong></ p> and I require this content to be displayed as regular view html

Any idea please about showing the content as html?

Thanks

like image 212
Mario Avatar asked Jun 08 '18 14:06

Mario


People also ask

Can I use template literals in HTML?

Template literals are great for HTML because you can add newlines and very cleanly have dynamic classes and other attributes.

Can you use backticks in HTML?

Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes. Besides, using backticks makes it easier for string operations.

Can you put an if statement inside a template literal?

You can't write anything that's not a simple expression inside of a template literal, so no if statements or loops. You can use ternary expressions, however, if you need to make a choice: var quantity = 14; var cart = `You have ordered ${quantity} ${quantity > 1 ?

What is HTML literal?

Tagged Template Literals A JavaScript template literal is a string literal that can have JavaScript expressions embedded in it: `My name is ${name}. ` The literal uses backticks instead of quotes, and can span multiple lines. The part inside the ${} can be any JavaScript expression.


1 Answers

The reason this happens is because DOMParser returns a HTMLDocument object and when you try to set HTMLDocument object as innerHTML of any element it calls toString() of this object - which is [object HTMLDocument].

You can try this yourself:

const html = '<div>Some html content</div>';

const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");

console.log(doc.toString()); // [object HTMLDocument]

Good news is, that in your case you don't need the DOMParser at all. All you need to unescape the given string and set it as innerHTML of your element:

// Your modified getHTML function
const getHTML = message => _.unescape(message).replace('\\', '');

// Container in which your want to render the HTML
const container = document.getElementById('container');

// Dummy HTML representing your data from the database
const html = _.escape('<h1>Your content</h1>');

// Dummy template representing your template literal
const template = `
<div>
  ${getHTML(html)}
</div>
`;

// Set the resolved dummy template as content of the dummy container
container.innerHTML = template;
<div id="container"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
like image 158
user3210641 Avatar answered Nov 02 '22 23:11

user3210641