views:

920

answers:

6

Hey there, I've got a block of HTML that I'm going to be using repeatedly (at various times during a users visit, not at once). I think that the best way to accomplish this is to create an HTML div, hide it, and when needed take its innerHTML and do a replace() on several keywords. As an example HTML block...

<div id='sample'>
  <h4>%TITLE%</h4>
  <p>Text text %KEYWORD% text</p>
  <p>%CONTENT%</p>
  <img src="images/%ID%/1.jpg" />
</div>

Would the best way to replace those keywords with dynamic data be to go...

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);

It just feels like I've chosen a stupid way to do this. Does anyone have any suggestions on how to do this faster, smarter, or better in any way? This code will be executed fairly often during a users visit, sometimes as often as once every 3-4 seconds.

Thanks in advance.

+1  A: 

I doubt there will be anything more efficient. The alternative would be splitting it into parts and then concatenating, but I don't think that would be much efficient. Perhaps even less, considering that every concatenation results in a new string which has the same size as its operands.

Added: This is probably the most elegant way to write this. Besides - what are you worried about? Memory usage? It's abundant and Javascript has a decent memory manager. Execution speed? Then you must have some gigantic string. IMHO this is good.

Vilx-
Thanks for the reply. In actuality this is a much bigger block with many more replaces, so before I started I wanted to make sure there wasn't something I was missing. Thanks again.
um... there is: chaining
annakata
And there are better ways to implement it.
some
A: 

You can make it more efficient by chaining the replaces instead of making all these interim assignments.

i.e.

with(document.getElementById('sample'))
{
  innerHTML = innerHTML.replace(a, A).replace(b, B).replace(c, C); //etc
}
annakata
Maybe, but doesn't this make readability worse? Although you might stack these calls vertically...
Vilx-
I believe you, but how does chaning change the performance?
chills42
-1, performance is the same
orip
putting this in a with block also will break if you're replacing a keyword with a variable name that is also an object property, like "id", for instance.
Triptych
*sigh* - look performance is *not* the same because chaining you create the object but do not assign it. For a chain N long you save N-1 assignments. Putting this in a with block certainly breaks if you have properties declared in scope of with, but I'm assuming as per the OP he's *not doing that*
annakata
@annakata, my benchmarks show no difference, do yours show one?. Since in JS assignment is just creating a reference, why should its time be non-neglibel?
orip
since you were kind enough to actually test it, so did I (something I haven't done for a few years). Long story short I did 10B operations of each mode and graphed it and chaining was ~2% faster, but the S.Dev was ~10%. Conclusion: close enough not to matter any more as bigger problems exist
annakata
+2  A: 

You could probably adapt this code to do what you want:

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, "")] || "";
});

You might also want to look into JavaScriptTemplates

Kristof Neirynck
+9  A: 

It looks like you want to use a template.

function template(templateid,data){
    return document.getElementById(templateid).innerHTML.replace(/%(\w*)%/g,function(){ return data[ arguments[1] ] || "";});
}

Explanation of the code:

  • Expects templateid to be the id of an existing element.
  • Expects data to be an object with the data.
  • Uses two parameters to replace to do the substitution:
  • The first is a regexp that searches for all %keys%. The key can be a combination of A-Z, a-z, 0-9 and underscore _.
  • The second is a anonymous function that gets called for every match.
  • The anonymous function searches the data object for the key that the regexp found. If the key is found in the data, then the value of the key is returned and that value will be replacing the key in the final output. If the key isn't found, an empty string is returned.

Example of template:

<div id="mytemplate">
  <p>%test%</p>
  <p>%word%</p>
</div>

Example of call:

document.getElementById("my").innerHTML=template("mytemplate",{test:"MYTEST",word:"MYWORD"});
some
+1  A: 

If you're willing to use the Prototype library, they have nice built in templating functionality.

That would look like:

element.innerHTML = (new Template(element.innerHTML)).evaluate({
    title: 'a title',
    keyword: 'some keyword',
    content: 'A bunch of content',
    id: 'id here'
})

This would be especially nice if you were running your code in a loop due to the ease of creating JSON objects/Javascript object literals.

Still, I wouldn't expect any speed increase.

Also, you would need to change your delimiter style to #{keyword} rather than %keyword%

Triptych
A: 

Your method is a standard way to implement a poor-man's templating system, so it's fine.

It could be worth your while to check out some JavaScript templating libraries, such as JST.

orip