Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 10 of 10
  1. #1
    Join Date
    May 2007
    Posts
    467
    Tokens
    0

    Default [PHP HELP] template system

    Okay i have been trying to make something like this for a long long time but never got it to work. i would like to know if someone can show me how to do this

    in a .tpl

    PHP Code:
    <-- if $member->group == admin -->

    adminCP link

    <-- endif -->                                                   

    <-- if 
    $member->group == vip -->

    stuff
    <-- elseif -->                                                   

    wow

    <-- endif --> 
    i know it has to use preg_replace etc but i am lose and need the help.

  2. #2
    Join Date
    Oct 2006
    Posts
    16
    Tokens
    0

    Default

    A good idea, I can get you started but you can learn the rest

    Read up on:
    file_get_contents()
    preg_replace()
    str_replace()
    eval()
    Regular Expression Matching

    // First, import the file contents into a variable
    $content = file_get_contents("PATH/TO/TEMPLATE.TPL");

    // Replace if with a php if
    $content = preg_replace("'<-- if (.*)-->'","<?if(\\1) {?>", $content);

    // Replace end if with php end if
    $content = str_replace("<-- endif -->","<?}?>", $content);

    // Lets run the outcome through the php engine again, and show the result.
    echo eval("?>$content<?");

    Why use str_replace second, as long as <-- endif --> always occurs EXACTLY like that you can easily replace it with }. str_replace is slightly faster than preg_replace so its best to use that where possible.

    This should get you started on the varibles you need to look at and the basic method of how you do it. I cannot guarntee the above will work (I haven't tested it) but the theory there should, so it maybe a case of trial and error to get this particular method to work. I should add that this is very basic but if you are really interested I am sure you can learn how to make it better and to your needs.

    Hope that helps in some way. Alternativly you could try and google "php template parsing" (I just did and saw 1 relevant result).

  3. #3
    Join Date
    Jun 2008
    Location
    Manchester
    Posts
    766
    Tokens
    0

    Default

    What the **** is that? You can't combine html conditional statements with php unless I'm missing something here.

  4. #4
    Join Date
    Jun 2005
    Posts
    4,795
    Tokens
    0

    Latest Awards:

    Default

    Use Smarty. Has what you want built into it.

  5. #5
    Join Date
    Sep 2005
    Location
    East London
    Posts
    3,028
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Jxhn View Post
    What the **** is that? You can't combine html conditional statements with php unless I'm missing something here.
    It's for a template. He wants to do like vbulletin do, <if blah blah> etc.

    It is possible using str_replace. fopen or w/e etc etc depending on template.

  6. #6
    Join Date
    May 2007
    Posts
    467
    Tokens
    0

    Default

    Quote Originally Posted by Anzrew View Post
    A good idea, I can get you started but you can learn the rest

    Read up on:
    file_get_contents()
    preg_replace()
    str_replace()
    eval()
    Regular Expression Matching

    // First, import the file contents into a variable
    $content = file_get_contents("PATH/TO/TEMPLATE.TPL");

    // Replace if with a php if
    $content = preg_replace("'<-- if (.*)-->'","<?if(\\1) {?>", $content);

    // Replace end if with php end if
    $content = str_replace("<-- endif -->","<?}?>", $content);

    // Lets run the outcome through the php engine again, and show the result.
    echo eval("?>$content<?");

    Why use str_replace second, as long as <-- endif --> always occurs EXACTLY like that you can easily replace it with }. str_replace is slightly faster than preg_replace so its best to use that where possible.

    This should get you started on the varibles you need to look at and the basic method of how you do it. I cannot guarntee the above will work (I haven't tested it) but the theory there should, so it maybe a case of trial and error to get this particular method to work. I should add that this is very basic but if you are really interested I am sure you can learn how to make it better and to your needs.

    Hope that helps in some way. Alternativly you could try and google "php template parsing" (I just did and saw 1 relevant result).
    Thank you ill +rep if i can.

    Quote Originally Posted by Tomm View Post
    Use Smarty. Has what you want built into it.
    I hate Smarty and i want to use a custom one.

    Okay so far i got this but i am unsure if its right.

    PHP Code:
    <?php

    class template 
    {
        
        var 
    $template_dir     '.templates/';
        var 
    $file_ext         '.tpl';
        var 
    $buffer;
        
        function 
    buff_template ($file
        {
            if( 
    file_exists$this->template_dir $file $this->file_ext ) )
            {
                
    $this->buffer file_get_contents$this->template_dir $file $this->file_ext );            
                return 
    $this->parse_variables($this->buffer);
            }
            else
            {
                die();
            }
        }
        
        function 
    parse_variables($input)
        {    
            
    $search preg_match_all('/<-- {.*?} -->/'$input$matches);
                
            foreach(
    $matches as $value)
            {
                
    $input preg_replace("'<-- if (.*) -->'","<? if(\\1) {?>"$input);
                
    $input preg_replace("'<-- foreach (.*) -->'","<? foreach(\\1) {?>"$input);
            }
            
            
    $content str_replace("<-- end -->","<?}?>"$content);
            
            return 
    $input;
        }

    }

    $engine = new template;

    echo eval(
    "?>$engine->buff_template(login)<?");
    ?>
    login.tpl
    HTML Code:
    <-- if $member->group == admin -->
    
    adminCP link
    
    <-- end -->                                                   
    
    <-- foreach $array as $value => $key -->
    
    key: $key value: $value
    
    <-- end -->

  7. #7
    Join Date
    Jun 2008
    Location
    Manchester
    Posts
    766
    Tokens
    0

    Default

    Ah, sorry I see what you mean now. The above code looks fine to me, aslong as you eval the parsed input. This could cause security issues if someone got into the template system though.
    Last edited by Jxhn; 29-12-2008 at 09:24 AM.

  8. #8
    Join Date
    Oct 2006
    Posts
    16
    Tokens
    0

    Default

    Hey VistaBoy,
    Your code that you gave looks correct, one small thing you need to change is the eval statement:

    echo eval("?>$engine->buff_template(login)<?");

    Needs to become:
    echo eval("?>".$engine->buff_template(login)."<?");

    That should then be fine, I haven't run it but it seems logically correct ! Just a matter of trial and error now.

  9. #9
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    One question, why are you echoing the eval out, outside of the class?

    Why can't you just do:

    PHP Code:
    echo $engine->buff_template'login' ); // echo since you return it 
    Why don't you eval the lines inside of the class, instead of having to call it outside of the script?

    You should just be able to call the template very simply, without having to enclose it in eval, if you get what I'm saying

  10. #10
    Join Date
    Dec 2006
    Location
    Scotland
    Posts
    1,365
    Tokens
    0

    Latest Awards:

    Default

    Smarty is your best option here, it works the way you want it, and its really easy to code for.

Posting Permissions

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