how to use javascript to parse HTML template

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
xudzh
Forum Commoner
Posts: 48
Joined: Sat Feb 19, 2005 6:22 pm

how to use javascript to parse HTML template

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, there are ways, but why do you want javascript to parse the template file?
xudzh
Forum Commoner
Posts: 48
Joined: Sat Feb 19, 2005 6:22 pm

Post 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.

:)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
xudzh
Forum Commoner
Posts: 48
Joined: Sat Feb 19, 2005 6:22 pm

Post by xudzh »

So there's no way to parse it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
xudzh
Forum Commoner
Posts: 48
Joined: Sat Feb 19, 2005 6:22 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
xudzh
Forum Commoner
Posts: 48
Joined: Sat Feb 19, 2005 6:22 pm

Post by xudzh »

Uh, is there an tutorial or book that you would recommand for optimizing php codes? performance is really important to me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply