Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14

Discussione: Template engine

  1. #1
    Utente bannato L'avatar di Skyline89
    Registrato dal
    Jun 2006
    Messaggi
    225

    Template engine

    Ciao a tutti, un mio amico mi ha creato un template engine nel suo massimo delle possibilità.

    Codice PHP:
    <?php

    class tplEngine
    {
        var 
    $tpl_file;
        var 
    $tpl_code;
        var 
    $tpl_output;
        var 
    $rpl_start;
        var 
    $rpl_end;

        function 
    tplEngine()
        {
            
    $this -> tpl_file "";
            
    $this -> tpl_code "";
            
    $this -> tpl_output "";
            
    $this -> rpl_start "{";
            
    $this -> rpl_end "}";
        }

        function 
    load($tpl_file)
        {
            if(empty(
    $tpl_file) || !file_exists($tpl_file) || !is_file($tpl_file))
            {
                return 
    false;
            }
            
            
    $this -> tpl_file $tpl_file;
            
            if(
    false !== ($handle fopen($tpl_file"r")))
            {
                
    $size filesize($tpl_file);
                
                if(
    false !== ($code fread($handle$size)))
                {
                    
    fclose($handle);
                    
    $this -> tpl_code $code;
                    
    $this -> tpl_output $code;
                    return 
    true;
                }
                
    fclose($fp);
            }
            return 
    false;
        }

        function 
    assign($var$value="")
        {
            if(empty(
    $this -> tpl_output))
            {
                return 
    false;
            }
            
            
    $replace $this -> rpl_start $var $this -> rpl_end;
            
    $this -> tpl_output str_replace($replace$value$this -> tpl_output);
            return 
    true;
        }
        
        function 
    ereg_assign($var$value="")
        {
            if(empty(
    $this -> tpl_output))
            {
                return 
    false;
            }
            
            
    $replace $var;
            
    $this -> tpl_output eregi_replace($replace$value$this -> tpl_output);
            return 
    true;
        }
        
        function 
    insert($var$inc_file=""$once=false)
        {
            if(empty(
    $inc_file) || !file_exists($inc_file) || !is_file($inc_file))
            {
                return 
    false;
            }
            
            
    $replace $this -> rpl_start $var $this -> rpl_end;
            if(
    substr_count($this -> tpl_output$replace) < 1)
            {
                return 
    false;
            }
            
            
    ob_start();
            if(
    $once == true)
            {
                include_once(
    $inc_file);
            }
            else
            {
                include(
    $inc_file);
            }
            
    $contents ob_get_contents();
            
    ob_end_clean();
            
            
            
    $this -> tpl_output str_replace($replace$contents$this -> tpl_output);
            return 
    true;
        }
        
        function 
    out()
        {
            echo 
    $this -> tpl_output;
        }
            
        function 
    reset()
        {
            
    $this -> tpl_output $this -> tpl_code;
        }
    }

    ?>
    e io mentre creo la mia pagina php e la mia html funziona tutto ma quando devo fare un ciclo, nn funziona.
    qualcuno sa come aiutarmi, oppure ha qualcosa di fatto? nn consigliatemi smarty o simili

  2. #2

    ............

    Ciao.
    Puoi usare questa:
    Codice PHP:
    <?php
    class Template
    {
        var 
    $__tplOutput;
        var 
    $__tplVars;
        function 
    Template($templateFile)
        {
            (
    file_exists($templateFile))?$this->__tplOutput=file_get_contents($templateFile):exit('Error:Template file '.$templateFile.' not found');
            
    $this->__tplVars = array();
        }
        function 
    assign($var_array)
        {
            
    // Make sure it is an array. 
            // Probably unneeded, but let's be safe.
            
    if (!is_array($var_array))
            {
                exit(
    'Error! assign() expects an array, string given');
            }
            
    $this->__tplVars $var_array;
        }

        
    /*
        Parse the template file
        @param  string  Template file
        @return string  Parsed template data
        */
        
    function parse()
        {
            foreach (
    $this->__tplVars as $var => $content)
            {
                
    $this->__tplOutput str_replace('{' $var '}'$content$this->__tplOutput);
            }
        }
        
    /*
        Output the template
        @param  string Template file
        @return string
        */
        
    function display()
        {
            return 
    $this->__tplOutput;
        }
    }

    ?>
    Implementazione:
    Istanzi la classe passandogli il file tpl
    richiami il metodo assign passandogli un array del
    genere array('replace'=>'valore')
    richiami il metodo parse e poi display facile no
    il file tpl una cosa del genere


    replace</p>



    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Utente bannato L'avatar di Skyline89
    Registrato dal
    Jun 2006
    Messaggi
    225
    Ciao, potresti spiegarmi meglio, con un esempio? che nn ho ben capito. grazie mille per la risposta e per la disponibiltà

  4. #4

    .......

    $objTpl = new Template("tplName.tpl.php");
    $array = array('name'=>'whisher');
    $objTpl->assign($array);
    $objTpl->parse();
    echo $objTpl->display();
    tpl


    name</p>


    Ti ritorna

    whisher</p>



    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    Utente bannato L'avatar di Skyline89
    Registrato dal
    Jun 2006
    Messaggi
    225
    Ciao,
    mi esce troppo complicato.
    se io dovrei fare una cosa del genere,
    Codice PHP:
    $dir './ciao';
        
    $wd opendir($dir);
        
    $bal = array();
        while(
    $bau readdir($wd)) 
        {
            if(
    is_dir("$dir/$bau") && $bau != "." && $bau != "..")
            {
                
    $bal $bau;
            }
        } 
    che mi dice le cartelle che si trovano nella directory ciao ma se è più di una con il mio tplengine nn riesco a stamparlo.
    e con la soluzione che mi hai dato nn capisco come si fa

  6. #6

    ...........

    Ops data l'ora tarda
    mi sono scordato le
    parentesi il template
    diventa così:




    {name}</p>

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  7. #7
    Utente bannato L'avatar di Skyline89
    Registrato dal
    Jun 2006
    Messaggi
    225
    si si di quello me ne ero accorto

  8. #8

    ...........

    Scusa ma cosa c'entra questo
    snippet con il template engine:

    Codice PHP:
    $dir './ciao';
        
    $wd opendir($dir);
        
    $bal = array();
        while(
    $bau readdir($wd))
        {
            if(
    is_dir("$dir/$bau") && $bau != "." && $bau != "..")
            {
                
    $bal $bau;
            }
        } 
    a parte che $bal va scritta in questo
    modo $bal[] qui trovi
    il modo giusto (il tuo è sbagliato) di trattare con le directories.
    Cosa ti serve recuperare o tenti di recuperare
    (già dubito che il codice recuperi qc) i file/directories dentro una directory
    consulta sempre il manuale !


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  9. #9
    Utente bannato L'avatar di Skyline89
    Registrato dal
    Jun 2006
    Messaggi
    225
    funziona benissimo, mi dice cosa c'è in quella cartella. xo con il template engine me ne fa vedere solo uno se no mi riscrive tutto il .tpl

  10. #10

    ..............

    Guarda il manuale e guarda cosa ho
    scritto ripeto cosa c'entra con un
    template engine.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.