...

View Full Version : Splitting template variables



thesavior
03-19-2007, 07:31 PM
Okay, here is what I want to do.

In my template files, I have variables in the format like this:


{user:username:}
{file:navbar:}
{config:logo:}
{lang:forum_by:}
{forum:icon:}
{breadcrumbs::}


etc.

What I want to do, is first have all variables in that format sent to a class called tpl_parse. Then inside tpl_parse I have functions based on each of the variable prefix. Ex: I have a function called "user", "file", "config", "lang", "forum". Each tpl variable should be sent and parsed by the correct function. How should I get these variables sent to the class/functions?

So if i run the above, they would be changed into:


$tpl_parse->user("username");
$tpl_parse->file("navbar");
$tpl_parse->config("logo");
$tpl_parse->lang("forum_by");
$tpl_parse->forum("icon");
$tpl_parse->breadcrumbs();

aedrin
03-19-2007, 07:59 PM
You parse out the variables using a regular expression.

Then do this for each match:



$type = $matches[1]; // what came before the colon
$variable = $matches[2]; // what came after the colon
$tpl_parse->$type($variable);


I don't know about the double colon for breadcrumbs. I'd just leave it empty after the colon. Easier to parse, and don't have to put in exceptions in your code either.

thesavior
03-19-2007, 10:27 PM
thanks alot, but what would be the regex to split the variable strings?

This is what I got, but Im not exactly sure what to do with it:



\{(.*):(.*)\:\}



preg_match("\{(.*)\:(.*)\:\}", $tpl_file, $variable)?

aedrin
03-19-2007, 10:33 PM
What's the second colon for?

You'd use this regular expression on the source string with preg_match().

thesavior
03-19-2007, 10:35 PM
the second colon is just in case i end up wanting to do something like:

{breadcrumb:add:home.php}

but for the preg match, is


preg_match("\{(.*)\:(.*)\:\}", $tpl_file, $variable)

correct?


a second question: How do I include the tpl file, parse then display to the screen. I know kinda how to parse it now, but the other things I dont. Also, how do I get it to parse through all the variables, I need to put the preg_match in a loop, but how do I set that up to get it to keep running from the beggining of a file, to the end. And say the included tpl file includes a file with includes in it, this needs to be completely recursive until there are no matches left.

thesavior
03-19-2007, 11:09 PM
my preg_match: preg_match("\{(.*)\:(.*)\:\}", $tpl_file, $variable)

is returning:



Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/tpl_parse.php on line 30

thesavior
03-20-2007, 03:36 PM
I have found that this regex works, however im getting warnings from my zend editor:


"/{(.*)\:(.*)\:\}/"


Bad escape sequence: \:
Bad escape sequence: \:
Bad escape sequence: :}


Any ideas?

aedrin
03-20-2007, 04:48 PM
Some PHP editors still aren't able to correctly parse out all PHP files out there.

Which in itself is kind of a sad fact considering PHP has been around for quite a while now. I understand that Eclipse has problems because it is free, but Zend is not free and you'd think they would have better code.

Which makes me wonder, since Eclipse PDT is being developed by the same people who write the Zend IDE. Why buy a closed source PHP editor, when there is a free version that is open source.

Why not make it easier on yourself, search for anything enclosed in { and } with your regular expression, then use PHP to explode on the : character. You'll only need 1 regular expression, and it'll be faster+reliable (this is what I use). Remember that Javascript also uses { } to enclose things. So either add another marker, for instance: {%include:test.php}, or just don't use Javascript in the pages :P I'd be curious to know how Smarty deals with this problem.

EDIT: I guess they allow you to change the delimiter.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum