Page 1 of 1
how to use javascript to parse HTML template
Posted: Tue Aug 23, 2005 9:51 pm
by xudzh
Hi,
If I have
Code: Select all
<script>
var stringA= " <b> {{name}}</b> -- {{title}}";
</script>
and
Code: Select all
<script>
var theArray= new Array();
theArray['name']="Bob";
theArray['title']="the title";
</script>
Is there a way to use theArray to parse stringA so that I'll get
<b>Bob</b> -- the title
Thanks
Posted: Wed Aug 24, 2005 1:26 am
by feyd
yes, there are ways, but why do you want javascript to parse the template file?
Posted: Thu Aug 25, 2005 2:54 am
by xudzh
On a php application where page requests are made almost every second. So if there are 100 people using the application at the same time on the same server it's gonna be a big server load. So the applicatoin will first load all the templates into the browser so that further http requests are only gonna need php to return the required variables instead of loading the whole page every time.

Posted: Thu Aug 25, 2005 8:25 am
by feyd
if 100 people can cripple your server, you need to optimize the code, upgrade the server, and/or potentially switch to a higher-tier host.
Posted: Thu Aug 25, 2005 3:16 pm
by xudzh
So there's no way to parse it?
Posted: Thu Aug 25, 2005 3:54 pm
by feyd
I never said that. Given a string of a template:
Code: Select all
<html>
<head>
</head>
<body>
<script language="Javascript">
function varMe()
{
var ret = arguments[0];
if(typeof(vars[arguments[1]]) != 'undefined')
{
ret = vars[arguments[1]];
}
return ret;
}
function parseTemplate(template)
{
var temp = template.toString();
temp = temp.replace(/\{\{(.*?)\}\}/g,varMe);
return temp;
}
templates = new Array();
templates[templates.length] = 'some {{template}} stuff';
templates[templates.length] = 'some {{more}} {{template}} stuff';
templates[templates.length] = 'some {{template}} {{stuff}}';
templates[templates.length] = 'some {{more}} {{template}} {{stuff}}';
vars = new Array();
vars['template'] = 'monkey';
vars['more'] = 'added';
vars['stuff'] = 'things';
for(var i in templates)
{
document.write('Original: '+templates[i]+'<br />\n');
document.write('Parsed: '+parseTemplate(templates[i],vars)+'<br /><br />\n');
}
</script>
</body>
</html>
outputs
Code: Select all
Original: some {{template}} stuff
Parsed: some monkey stuff
Original: some {{more}} {{template}} stuff
Parsed: some added monkey stuff
Original: some {{template}} {{stuff}}
Parsed: some monkey things
Original: some {{more}} {{template}} {{stuff}}
Parsed: some added monkey things
you'll need an XMLHTTP engine or other partial request generators of some fashion to transport the variables...
Posted: Thu Aug 25, 2005 4:25 pm
by programmermatt
What I think is important to add here is that xmlHTTP does not actually greatly decrease server load, as generally all the code that would run if it were a normal page load would run for a xmlHTTP request. Where the big kicker with xmlHTTP is, at least in my humble opinion, is bandwidth and UI. Sure I have to inform all my users that they shouldn't expect the whole page to reload (as they tend to hit refresh when nothing seems to be happening, you would think they would notice blocks of text changing..), but that portions will be updated at a time. It is still worth the hassle. And of course, it should always degrade nicely... or nicely enough

Posted: Thu Aug 25, 2005 4:46 pm
by feyd
that's what I was getting at earlier. Time is better spent optimizing the pages more, reducing the amount of database queries, and such.
Posted: Wed Aug 31, 2005 12:05 pm
by xudzh
Thanks for the code. I changed it a little and it's working perfectly.
Uh, what's xmlHttp?
Also, wouldn't the server require less resources if there are less to output?
Posted: Wed Aug 31, 2005 12:17 pm
by feyd
the memory used by an output is often quite small, compared to the space occupied by the database and time required for it to return data and various other things..
Posted: Fri Sep 02, 2005 10:18 pm
by xudzh
Uh, is there an tutorial or book that you would recommand for optimizing php codes? performance is really important to me.
Posted: Fri Sep 02, 2005 10:24 pm
by feyd