Page 1 of 1

preg_replace() template parsing woes

Posted: Wed Jun 02, 2010 7:38 pm
by mecha_godzilla
Hopefully I can explain this well enough to get an answer...

I'm trying to create a basic template parser to use in one of my scripts - the idea is that it will load in a text file that looks something like this:

Code: Select all

<html>
<head>
<title>Title goes here</title>
</head>
<body>

<!-- BEGIN type="my_paragraph" -->
<p>{my_paragraph}</p>
<!-- END -->

</body>
</html>
and I then use this code to extract the value between the type="" attribute:

Code: Select all

preg_match('#<!-- BEGIN type="(.*?)" -->#', $this_line_of_code[$i], $matches);
$name_of_type = '{' . $matches[1] . '}';
What I now want to do is use preg_replace to replace the {my_paragraph} text with a value from an array called my_paragraph[]. With the code I've created so far (which I won't post because it's a bit of a mess at the moment) I can generate as many paragraphs as there are values in the my_paragraph[] array but I don't want to hard-code this value when I use preg_replace, I want to be able to use whatever value happens to be between the type="" attribute as the array name.

At the moment, my preg_replace code looks like this:

Code: Select all

$my_parsed_code[$line++] .= preg_replace("/$name_of_type/", $my_paragraph[$i], $this_line_of_code[$i]);
but what I need to do is replace the $my_paragraph[$i] part of the function with a 'generated' version based on whatever type="" contains. I tried this:

Code: Select all

$test_value = '$' . "$matches[1]" . '[' . "$i" . ']';
$my_parsed_code[$line++] .= preg_replace("/$name_of_type/", "$test_value", $this_line_of_code[$i]);
but it doesn't interpret $test_value it just outputs it as-is where the paragraph text is supposed to appear. I figure there's probably some really clever tokens/runes that I need to do what I want but I don't know what they are and/or whether preg_replace can work in this way.

Any help for this rather tedious and involved question would be gratefully received :)

Mecha Godzilla

Re: preg_replace() template parsing woes

Posted: Thu Jun 03, 2010 5:21 am
by markusn00b
You may not know this, but PHP can be mixed right in with your HTML. Yeah. Crazy, huh?

Seriously, though, a templating engine on-top of PHP is as redundant as... well, I can't think of an example, but it's incredibly redundant, and makes little sense.

I apologise for this unhelpful reply, but I don't like to see folk making things harder than they should be.

Re: preg_replace() template parsing woes

Posted: Thu Jun 03, 2010 9:55 am
by AbraCadaver
I somewhat agree with markusn00b, but here is some help:

Code: Select all

$my_parsed_code[$line++] .= preg_replace("/$name_of_type/", ${$matches[1]}[$i], $this_line_of_code[$i]);
It may be cleaner to do this:

Code: Select all

$name_of_type = $matches[1];
$my_parsed_code[$line++] .= preg_replace('/{'.$name_of_type.'}/', ${$name_of_type}[$i], $this_line_of_code[$i]);
For this simple matching str_replace() will be much faster:

Code: Select all

$name_of_type = $matches[1];
$my_parsed_code[$line++] .= str_replace('{'.$name_of_type.'}', ${$name_of_type}[$i], $this_line_of_code[$i]);

Re: preg_replace() template parsing woes

Posted: Thu Jun 03, 2010 4:24 pm
by mecha_godzilla
Thank you for the replies - well, at least the helpful one :)

The front-end for the site I am working on is being designed by a web designer who - get this - isn't interested in dealing with a bunch of PHP code. Who'd have thought it?

Unless I'm mistaken, I seem to remember something about "the logical separation of content and style" being one of the underlying goals that has defined new web standards over the past 10 years or so. But what's this - apparently the professional approach is just to sling a load of PHP, XHTML and JavaScript in one big ol' file and hope that no-one apart from me wants to edit that bad boy! :mrgreen:

M_G

Re: preg_replace() template parsing woes

Posted: Thu Jun 03, 2010 5:00 pm
by markusn00b
mecha_godzilla wrote:Thank you for the replies - well, at least the helpful one :)

The front-end for the site I am working on is being designed by a web designer who - get this - isn't interested in dealing with a bunch of PHP code. Who'd have thought it?

Unless I'm mistaken, I seem to remember something about "the logical separation of content and style" being one of the underlying goals that has defined new web standards over the past 10 years or so. But what's this - apparently the professional approach is just to sling a load of PHP, XHTML and JavaScript in one big ol' file and hope that no-one apart from me wants to edit that bad boy! :mrgreen:

M_G
Hey, no need for the attitude. We're all friends.

What, you think using a Templating library is abstracting logic out of your presentation? That's ridiculous. You're substituting one syntax with another. And, while doing so, adding extra, unnecessary overhead.

Anyway, you stick to these buzz-concepts of web-programming, perpetuated by misinformed folk like yourself.

Mark.

Re: preg_replace() template parsing woes

Posted: Fri Jun 04, 2010 10:03 am
by Jonah Bron
There are better ways to separate content and style (the include function being a dominant one). This is just re-inventing the wheel, and there's a good reason that you've likely never heard of this approach before.

Re: preg_replace() template parsing woes

Posted: Fri Jun 04, 2010 11:42 am
by omniuni
Sometimes templating can be useful. For example, rather than having ANY php code, you just say "type this, and it will magically happen."

One very simple way I've done this, is to use a single tag, like this:

%DYN_PARAGRAPH%

This is extremely simple to work with using PHP's str_replace() function. I also use this for formatting data returned from a database, so if I call a function in my class to display results, I can pass it a string with tags in it, and it returns a string with HTML formatted they way I want it:

$USER_CLASS->list_users('User with ID %I%, and name %L%, %F%, registered on %D%.<br/>');

And the returned string has %I% replaced with the ID, %L% and %F% replaced with the last and first name respectively, and %D% replaced with their registration date. It returns as many users as there are. I find it to be a very efficient solution.