Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    Splitting template variables

    Okay, here is what I want to do.

    In my template files, I have variables in the format like this:
    Code:
    {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:
    Code:
    $tpl_parse->user("username");
    $tpl_parse->file("navbar");
    $tpl_parse->config("logo");
    $tpl_parse->lang("forum_by");
    $tpl_parse->forum("icon");
    $tpl_parse->breadcrumbs();
    Last edited by thesavior; 03-19-2007 at 06:52 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You parse out the variables using a regular expression.

    Then do this for each match:

    PHP Code:
    $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.

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    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:

    Code:
    \{(.*):(.*)\:\}
    Code:
    preg_match("\{(.*)\:(.*)\:\}", $tpl_file, $variable)
    ?
    Last edited by thesavior; 03-19-2007 at 09:34 PM.

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    What's the second colon for?

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

  • #5
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    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

    Code:
    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.
    Last edited by thesavior; 03-19-2007 at 10:02 PM.

  • #6
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    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
    Last edited by thesavior; 03-19-2007 at 10:13 PM.

  • #7
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    I have found that this regex works, however im getting warnings from my zend editor:

    Code:
    "/{(.*)\:(.*)\:\}/"
    Bad escape sequence: \:
    Bad escape sequence: \:
    Bad escape sequence: :}
    Any ideas?

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.
    Last edited by aedrin; 03-20-2007 at 03:51 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •