Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div id='sample'>
- <h4>%TITLE%</h4>
- <p>Text text %KEYWORD% text</p>
- <p>%CONTENT%</p>
- <img src="images/%ID%/1.jpg" />
- </div>
- template = document.getElementById('sample');
- template = template.replace(/%TITLE%/, some_var_with_title);
- template = template.replace(/%KEYWORD%/, some_var_with_keyword);
- template = template.replace(/%CONTENT%/, some_var_with_content);
- template = template.replace(/%ID%/, some_var_with_id);
- //Updated 28 October 2011: Now allows 0, NaN, false, null and undefined in output.
- function template(templateid,data){
- return document.getElementById(templateid).innerHTML.replace(/%(w*)%/g,function(m,key){return data.hasOwnProperty(key)?data[key]:"";});
- }
- <div id="mytemplate">
- <p>%test%</p>
- <p>%word%</p>
- </div>
- document.getElementById("my").innerHTML=template("mytemplate",{test:"MYTEST",word:"MYWORD"});
- var user = {
- "firstName": "John",
- "login": "john_doe",
- "password": "test",
- };
- var textbody = ""
- +"Hey {firstName},n"
- +"n"
- +"You recently requested your password.n"
- +"login: {login}n"
- +"password: {password}n"
- +"n"
- +"If you did not request your password, please disregard this message.n"
- +"";
- textbody = textbody.replace(/{[^{}]+}/g, function(key){
- return user[key.replace(/[{}]+/g, "")] || "";
- });
- element.innerHTML = (new Template(element.innerHTML)).evaluate({
- title: 'a title',
- keyword: 'some keyword',
- content: 'A bunch of content',
- id: 'id here'
- })
- with(document.getElementById('sample'))
- {
- innerHTML = innerHTML.replace(a, A).replace(b, B).replace(c, C); //etc
- }
Add Comment
Please, Sign In to add comment