Results 1 to 4 of 4
  1. #1
    Join Date
    May 2006
    Posts
    1,797
    Tokens
    0

    Latest Awards:

    Default [PHP] Templating Engine Helppp ;[

    Ok, basically this is the first time ive EVER made a templating engine, and as a result, having a few problems with it.

    The commented lines are just the things ive taken out so i can test it without database access.

    template.inc.php
    PHP Code:
    <?php
        
    #require("dbconfig.php");
        
        #$fetch = mysql_fetch_array(mysql_query("select * from `dpan_config` where `asp` = 'template'"));
        #$template = $fetch["pref"];
        
    $template "default";
        
            if(
    file_exists("skins/".$template."/login.htm"))
            
    $login_template "skins/"$template ."/login.htm";
            elseif(
    file_exists("skins/".$template."/login.html"))
            
    $login_template "skins/"$template ."/login.html";
            elseif(
    file_exists("skins/".$template."/login.php"))
            
    $login_template "skins/"$template ."/login.php";
            else
            die(
    "INVALID TEMPLATE SPECIFIED.");
            
            if(
    file_exists("skins/".$template."/main.htm"))
            
    $main_template "skins/"$template ."/main.htm";
            elseif(
    file_exists("skins/".$template."/main.html"))
            
    $main_template "skins/"$template ."/main.html";
            elseif(
    file_exists("skins/".$template."/main.php"))
            
    $main_template "skins/"$template ."/main.php";
            else
            die(
    "INVALID TEMPLATE SPECIFIED.");
            
            function 
    compiletemplate($type)
            {
                if(
    $type == "login")
                {
                
    $getfile file_get_contents($login_template);
                
                    
    #$panel_message = mysql_fetch_array(mysql_query("select * from `dpan_config` where `asp` = 'panel_message'"));
                    #$panel_message = $panel_message["pref"];
                    
    $panel_message "Heyyy";
                    
    $getfile str_replace("{panel_message}",$panel_message,$getfile);
                    
                    
    $logincode '<form action="?login=login" method="post">
        <div align="center">
        Username:<br />
        <input type="text" name="username" /><br /><br />
        Password:<br />
        <input type="password" name="password" /><br /><br />
        <input type="submit" value="Login" />
        </div>
        </form>'
    ;
                    
    $getfile str_replace("{logincode}",$logincode,$getfile);
                    
                    echo(
    $getfile);
                }
                elseif(
    $type == "main")
                {
                
    $getfile file_get_contents($main_template);
                
                }
                else
                die(
    "NO TEMPLATE TYPE SPECIFIED.");
            }
            
    ?>
    index.php
    PHP Code:
    <?php
        
    require("inc/template.inc.php");
        
    compiletemplate('login');
    ?>
    (Il add html tags etc to it later, was just checking it worked before i continued any further with it)


    Error is that the page is not showing at all.
    (Like a blank page)

    Thanks for any help anyone may have. !
    Last edited by MrCraig; 04-09-2008 at 04:11 PM.
    Coming and going...
    Highers are getting the better of me

  2. #2
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    PHP Code:
    <?php
    class template
    {
        private 
    $file$template$page;
        public function 
    parse$template$file$page )
        {
            
    $this->template $template$this->file $file$this->page $page;
            
    $template "templates/" $template "/" $file ".tpl";
            if( !
    file_exists($template) )
            {
                exit ( 
    "Cant find template file");
            } else
            {
                
    $template file_get_contents$template );
                
    $template $this->custom_vars$template );
                return 
    $template;
            }
        }
        private function 
    custom_vars$template )
        {
            
    $matches = array();
            if( 
    preg_match_all"#\\{([A-Za-z_]+)\\}#ie"$template$matches ) )
            {
                for( 
    $i 0$i count($matches) - 1$i++ )
                {
                    
    $template preg_replace('#\\{([A-Za-z_]+)\\}#ie''$this->get_var("\\1")'$template);
                }
            }
            return 
    $template;
        }
        private function 
    get_var$var )
        {
            
    $template "templates/" $this->template "/" $var ".tpl";
            if( !
    file_exists($template) )
            {    
                
    $core->user = new user;
                if( 
    $var == "TITLE" )
                {
                    
    $return $this->status$this->page );
                } elseif( 
    $var == "CONTENT" )
                {
                    
    $template "templates/" $this->template "/pages/" $this->page ".tpl";
                    if( 
    file_exists($template) )
                    {
                        
    $return file_get_contents($template);
                    }
                } elseif( 
    $var == "PAGE_TITLE" )
                {
                    if( 
    $core->user->check_login() === true )
                    {
                        if( 
    $this->page == "loggedin" )
                        {
                            
    $page "main";
                        } else {
                            
    $page $this->page;
                        }
                    } else
                    {
                        
    $page $this->page;
                    }
                    
    $return "hi";
                } elseif( 
    $var == "HEADER" )
                {
                    if( 
    $core->user->check_login() === true )
                    {
                        
    $return "Welcome to BLANK, <strong>" $_SESSION["username"] . "</strong>, you last logged in at " $core->user->lastonline();
                    } else
                    {
                        
    $return "Oh noes! Your not logged in!";
                    }
                }
            } else
            {
                
    $return file_get_contents$template );
            }
            return 
    $return;
        }
    }
    ?>
    tbh they should submit their own template to be parsed within etc
    Last edited by Blob; 04-09-2008 at 04:34 PM.

  3. #3
    Join Date
    May 2006
    Posts
    1,797
    Tokens
    0

    Latest Awards:

    Default

    Umm thanks ryan but i really only want to know whats wrong with existing code..
    Coming and going...
    Highers are getting the better of me

  4. #4
    Join Date
    May 2006
    Posts
    1,797
    Tokens
    0

    Latest Awards:

    Default

    NVM, got it working
    Coming and going...
    Highers are getting the better of me

Posting Permissions

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