Skip to content

Instantly share code, notes, and snippets.

@coder054
Created December 28, 2020 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coder054/f0ffa63f178b5334268261465e80450c to your computer and use it in GitHub Desktop.
Save coder054/f0ffa63f178b5334268261465e80450c to your computer and use it in GitHub Desktop.
type TemplateType = {
template: string;
id: string;
};
function withTemplate(templateObj: TemplateType) {
return function (constructor: any) {
let el = document.getElementById(templateObj.id);
let p = new constructor();
if (el) {
let content = templateObj.template.replace(/{{\w+}}/g, function (a1) {
let property = a1.replace("{{", "").replace("}}", "");
return p[property];
});
el.innerHTML = content;
}
};
}
@withTemplate({ template: `<h1>Name: {{name}}, Age: {{age}}</h1>`, id: "app" })
class Person {
name = "tuan anh";
age = 28;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment