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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to parse template file ??

    Hello forums !!
    I have some file named "test.tpl"
    whic has following contents
    "Hello %name% your passwrod is %passwrod%"
    I would like to call this file and replace those values within % with dynamic values(may also {}).
    Any comments and suggestions are warmly welcome.
    Thanks in advance to all of you

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    sams publishing quick template class, looks something like this. i don't know is it ok to post it here

    PHP Code:
    <?php
        
    class quick_template {

            private 
    $t_def;

            public function 
    parse_template($subset 'main') {

                
    $noparse false;
                
    $content "";
                
    $temp_file $this->t_def[$subset]['file'];

                if(isset(
    $temp_file)) {

                    if(
    strlen($temp_file) > 6) {
                        
    substr($temp_filestrlen($temp_file)-6);
                    }

                    if(
    strcasecmp($ext".thtml") != 0) {
                        
    $noparse true;
                    }

                    if(!
    $fr) {
                        
    $content "<!-- Error loading '$temp_file' //-->";
                    } else {
                        
    $content fread($frfilesize($temp_file));
                    }

                    @
    fclose($fr);

                } else {

                    if(isset(
    $this->t_def[$subset]['content'])) {
                        
    $content $this->t_def[$subset]['content'];
                    } else {
                        
    $content "<!-- Content for '$subset' not defined //-->";
                    }

                }

                if(!
    $noparse) {

                    
    $content=preg_replace("/\%([A-Z]*)\%/e",
                    
    "quick_template::parse_template(strtolower('$1'))",
                    
    $content);
                }

                return 
    $content;

            }

            function 
    __construct($temp='') {

                if(
    is_array($temp)) $this->t_def $temp;

            }
        }
    ?>


  •  

    Posting Permissions

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