how to embed html code in template in polymer3 (like unsafeHTML)

616 Views Asked by At

please check the plunker

static get template() {
   return html`
     <p>This content is from ChildClass.</p>
     <p>${super.template}</p>
     <p>Hello again from ChildClass.</p>
     <p style='color:red'>[[partHtml]] <==== this should be 'hello'</p>
     `;  
 }
 get partHtml()
 {
   return "<span>hello</span>";
 }

I want partHtml to be injected into a template like normal HTML.

I know unsafe HTML in lit-element can do that, but lit-element just can't use super.render() things, it is not convenient like polymer-element.

3

There are 3 best solutions below

0
Kent Wood On BEST ANSWER

Finally, I found the answer when I debug into the call stack. just use htmlLiteral, key code likes this

import {PolymerElement, html} from 'https://unpkg.com/@polymer/[email protected]/polymer-element.js'
import {htmlLiteral} from 'https://unpkg.com/@polymer/[email protected]/lib/utils/html-tag.js'

....

  static get template() {
      return html`<p style='color:red'>${this.partHtml} <==== this should be 'hello'</p>`;  
  }
  static get partHtml()
  {
    return htmlLiteral `<span>hello</span>` ;
  }
1
Hyyan Abo Fakher On

How about using inner-h-t-m-l attribute

static get template() {
   return html`
     <p>This content is from ChildClass.</p>
     <p>${super.template}</p>
     <p>Hello again from ChildClass.</p>
     <p style='color:red' inner-h-t-m-l="[[partHtml]]"></p>
     `;  
 }
 get partHtml()
 {
   return "<span>hello</span>";
 }
0
Pascal L. On

You can use multiline String

static get template() {
  return html`
   <p>This content is from ChildClass.</p>
   <p>${super.template}</p>
   <p>Hello again from ChildClass.</p>
   <p style='color:red'>${this.partHtml}</p>
 `;  
}
static get partHtml() {
  return html`<span>hello</span>`;
}

Test on Plnkr