Ankündigung

Einklappen
Keine Ankündigung bisher.

Übersicht Template-Systeme?

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Übersicht Template-Systeme?

    Servus,

    ich bin auf der Suche nach einen geeigneten Template-System.
    Ich möchte kein eigenes Template-System entwickeln um das gleich vorweg zu nehmen.
    Wenn jemand gute Template-Engine kennt bitte ich denjenigen dies mal hier zu nennen oder evl. sogar zu verlinken.

    Ich find persöhnlich das phpsavant recht gut aber leider wird das ja anscheinend nicht mehr weiterentwickelt.

  • #2
    Smarty wäre eine alternative, gibts unter www.smarty.net

    Da hast du, aber im Gegensatz zu "Savant", welches ich nicht kenne, aber eine "neue" Syntax.

    Ansonsten ist Smarty recht mächtig. Vor allem praktisch wenn du externe Designer hast. Die haben dann nur eingeschränkte Rechte und können kein PHP im Template ausführen!

    Kommentar


    • #3
      Eingeschränkte Rechte müssen aber erst konfiguriert werden. Standardmäßig sind {php}-Tags und Konsorten nämlich aktiviert.

      Kommentar


      • #4
        Hallo Scogit,

        ich kann dir in diesem Zusammenhang das Adventure PHP Framework empfehlen. Mit dem Framework hast du nicht nur Templating in gegenüber Smarty erweiterten Form, sondern auch noch einige Features mehr, die dir helfen, Applikationen mit state-of-the-art Entwicklungsmethoden zu ersellen. Templating ist meiner langjährigen Erfahrung nach nur ein Thema, das bei der Erstellung von Applikationen wichtig ist. Konkrete Informationen findest du unter http://www.adventure-php-framework.org/Seite/Templates. Bei Fragen kannst du dich gerne an mich wenden.

        Kommentar


        • #5
          Ich möchte dir nicht zu nahe treten aber das Template-System sieht viel zu überladen aus und auch dort muss du extra für das Template-System eine neue Scriptsprache erlernen ... PHPSavant war da schön schlicht nur leider gibt es keine wirklichen Releases mehr von PHPSavant.

          Kommentar


          • #6
            Von welchem Templatesystem sprichst du? Das vom APF oder Smarty?

            Wenn du ein Templatesystem haben möchtest dass du zusätzliche Funktionen bietet dann musst du zwangsweise immer eine neue Syntax lernen!

            Wenn du keine neue Syntax möchtest dann kannst du entweder einfach eine PHP Datei per include() einfügen in der dann einfach HTML mit ein paar PHP Anweisungen steht. Dabei solltest du den PHP Code so knapp wie möglich halten und wirklich nur Variablen ausgeben und Schleifen/Abfragen nutzen!

            Möchtest du dann einen Tick mehr Funktionalität kannst du dir eine kleine Klasse basteln die ebenfalls eine PHP Datei einbindet aber z.B. noch so etwas wie Caching bietet.

            Ich hab mal hier ne kleine Klasse geschrieben die kannst du dir gerne mal anschauen.

            Sie bietet folgende Funktionen:

            PHP-Code:
            <?php
            $tpl 
            = new SimplePhpTemplate();

            // konfigurieren, diese Werte würden auch als Standard genommen
            $tpl->template_dir 'template';
            $tpl->cache_dir 'cache';

            // Variable zuweisen
            $tpl->assign('value''Testwert'); // oder Variable als Referenz per assign_by_reference();

            // Template ausgeben
            $tpl->display('file.tpl');

            // oder Template in eine Variable schreiben
            $tpl->fetch('file.tpl');

            // oder nutzen wir mal Caching
            if (!$tpl->is_cached('file.tpl')) {
                
            // hole z.B. Werte aus der DB oder berechne komplexe Dinge
                // diese Dinge dann auch dem Template zuweisen (assign())
            }
            $tpl->caching true;
            $tpl->cache_lifetime 60*60// eine Stunde cachen
            $tpl->display('file.tpl'); // diese Datei wird jetzt gecached

            // Cache wieder löschen
            $tpl->delete_cache_file('file.tpl');

            // oder den kompletten Cache löschen
            $tpl->delete_cache();

            /*
            Beim Caching kann noch ein zweiter Parameter an display(), fetch(), is_cached(), delete_cache_file() als Array übergeben werden. Dieser ist dafür da mehrere Versionen einer Datei zu cachen. Z.B. wenn z.B. bei einem Blog die Seite mit den Einträgen gecached werden soll. Dann kann z.B. als weiterer Parameter die aktuelle Seite übergeben werden.
            $tpl->display('blog.tpl', array($page));
            Jetzt wird, je nach angabe in $page, ein anderer Cache genutzt. Sonst würde für jede Seite die selbe Cachefile genutzt werden die erstellt wurde.
            */
            ?>
            Funktioniert recht ähnlich wie Smarty ist aber deutlich schlanker!
            Die Klasse dürfte teilweise noch ein paar Fehler enthalten aber so ungefähr dürfte es funktionieren!

            Hier die Klasse.

            PHP-Code:
            <?php
            // TODO: missing echo in "display(), else{}"???

            /**
             * SimplePhpTemplate class
             * this is a simple template class, no extra syntax just uses php and
             * "include()" to be simple and fast but provides caching of template files
             */
            class SimplePhpTemplate {


                
            /**
                 * where are the template files (no slash at the end)
                 *
                 * @var string
                 */
                
            public $template_dir './template';


                
            /**
                 * where to put the cache files (no slash at the end)
                 *
                 * @var string
                 */
                
            public $cache_dir './cache';


                
            /**
                 * the timestamp the class should use for caching
                 *
                 * @var int  timestamp
                 */
                
            public $time;


                
            /**
                 * the extension for the cache file
                 *
                 * @var string
                 */
                
            public $cache_extension '.cache';


                public 
            $caching FALSE;
                public 
            $cache_lifetime 18000;


                private 
            $template_vars = array();
                private 
            $error_reporting;


                
                public function 
            __construct() {
                    
            $this->time time();
                }



                
            /**
                 * assigns values to template variables
                 *
                 * @param  string|array $name
                 * @param  string       $value
                 * @return void
                 */
                
            public function assign($name$value null) {

                    
            // assign just a single string
                    
            if (is_string($name)) {
                        
            $array = array($name => $value);

                    
            // assign a whole array
                    
            } else if (is_array($name)) {
                        
            $array $name;

                    }


                    
            // loop the array and assign each pair key => value
                    
            foreach ($array as $name => $value) {

                        
            // forbidden name
                        
            if ($name == 'this') {
                            
            trigger_error('Template: Cannot assign a variable called "this"!'E_USER_NOTICE);

                        
            // assign the variable
                        
            } else if ($name != '') {
                            
            $this->template_vars[$name] = $value;
                        }

                    }

                }



                
            /**
                 * assigns values to template variables by reference
                 *
                 * @param  string|array $name
                 * @param  string       $value
                 * @return void
                 */
                
            public function assign_by_reference($name, &$value null) {

                    
            // assign just a single string
                    
            if (is_string($name)) {
                        
            $array = array($name => &$value);

                    
            // assign a whole array
                    
            } else if (is_array($name)) {
                        
            $array $name;

                    }


                    
            // loop the array and assign each pair key => value
                    
            foreach ($array as $name => &$value) {

                        
            // forbidden name
                        
            if ($name == 'this') {
                            
            trigger_error('Template: Cannot assign a variable called "this"!'E_USER_NOTICE);

                        
            // assign the variable
                        
            } else if ($name != '') {
                            
            $this->template_vars[$name] = &$value;
                        }

                    }

                }



                
            /**
                 * displays a template file
                 * if caching is enabled, the
                 * function will use the cached version
                 * if it is valid
                 *
                 * @param  string $file
                 * @param  array  $cache_id
                 * @return void
                 */
                
            public function display($file$cache_id = array()) {

                    
            // caching is enabled validate the cache file
                    
            if ($this->caching === TRUE) {

                        
            // there is a cached template do not create a new one
                        
            if ($this->is_cached($file$cache_id)) {
                            
            $this->get_cache($file$cache_id);

                        
            // there is no cached template generate a new one
                        
            } else {
                            
            $cache $this->get_template($fileFALSE);

                            
            // write a cache file
                            
            $this->write_cache($file$cache_id$cache);

                            
            // and put out the template
                            
            echo $cache;

                        }

                    
            // caching is disabled so put out what we got
                    
            } else {

                        
            $this->get_template($file);

                    }

                }



                
            /**
                 * fetchs a template file
                 * if caching is enabled, the
                 * function will use the cached version
                 * if it is valid
                 *
                 * @param  string $file
                 * @param  array  $cache_id
                 * @return string $template
                 */
                
            public function fetch($file$cache_id = array()) {

                    
            // caching is enabled validate the cache file
                    
            if ($this->caching === TRUE) {

                        
            // there is a cached template do not create a new one
                        
            if ($this->is_cached($file$cache_id)) {
                            return 
            $this->get_cache($file$cache_idFALSE);

                        
            // there is no cached template generate a new one
                        
            } else {
                            
            $cache $this->get_template($fileFALSE);

                            
            // write a cache file
                            
            $this->write_cache($file$cache_id$cache);

                            
            // and put out the template
                            
            return $cache;

                        }

                    
            // caching is disabled so put out what we got
                    
            } else {

                        return 
            $this->get_template($fileFALSE);

                    }
                }



                
            /**
                 * checks if there is a cached template
                 *
                 * @param  string  $file
                 * @param  array   $cache_id
                 * @return boolean $result
                 */
                
            public function is_cached($file$cache_id = array()) {

                    
            $cache_file $this->cache_dir.'/'.$this->generate_cache_name($file$cache_id);

                    
            // is caching enabled and does a cached file exist
                    
            if ($this->caching === TRUE AND file_exists($cache_file) AND is_readable($cache_file)) {
                        
            $cache = @file_get_contents($cache_file);

                        if (
            $cache !== FALSE) {
                            
            $cache unserialize($cache);

                            
            // the cache file is too old
                            
            if ($cache !== FALSE AND
                                
            is_array($cache) AND
                                
            count($cache) == AND
                                
            $cache[0] != AND
                                
            $cache[0] < $this->time)
                            {
                                
            $this->delete_cache_file($file$cache_id);
                            } else {

                                
            // we have a valid cache file
                                
            return TRUE;

                            }

                        }

                    }

                    
            // there is no valid cache file
                    
            return FALSE;

                }



                
            /**
                 * deletes a single cache file
                 *
                 * @param  string $file
                 * @param  array  $cache_id
                 * @return void
                 */
                
            public function delete_cache_file($file$cache_id = array()) {

                    
            $cache_file $this->cache_dir.'/'.$this->generate_cache_name($file$cache_id);

                    if (
            file_exists($cache_file) AND is_readable($cache_file)) {
                        
            unlink($cache_file);
                    }
                }



                
            /**
                 * deletes all cache files
                 *
                 * @return void
                 */
                
            public function delete_cache() {

                    
            $files glob($this->cache_dir.'/*'.$this->cache_extension);

                    foreach (
            $files as $cache_file) {
                        if (
            file_exists($cache_file) AND is_readable($cache_file)) {
                            
            unlink($cache_file);
                        }
                    }
                }



                
            /**
                 * includes the template and
                 * displays it or returns the template
                 * as a string
                 *
                 * @param  string  $file
                 * @param  boolean $display
                 * @return string  $template if $display is set to FALSE
                 */
                
            private function get_template($file$display TRUE) {

                    
            // template file does not exist
                    
            if (!file_exists($this->template_dir.'/'.$file)) {
                        
            trigger_error('Template: '.$this->template_dir.'/'.$file.' does not exist!'E_USER_ERROR);

                    } else {

                        
            // extract the template_vars
                        
            foreach ($this->template_vars as $name => $value) {
                            $
            $name $value;
                        }


                        
            // start output buffer if content should be returned
                        
            if ($display === FALSE) {
                            
            ob_start();
                        }


                        
            // deactivate E_NOTICE, not initialized variables should not throw a notice
                        
            $this->error_reporting error_reporting(E_ALL E_NOTICE);


                        
            // write the template into the buffer
                        
            include($this->template_dir.'/'.$file);


                        
            // reset the error_reporting level
                        
            error_reporting($this->error_reporting);


                        
            // return the template and clean the buffer
                        
            if ($display === FALSE) {
                            
            $template ob_get_contents();
                            
            ob_end_clean();

                            return 
            $template;
                        }

                    }
                }



                
            /**
                 * get a cached file and
                 * displays it or returns the
                 * template as an array
                 *
                 * @param  string  $file
                 * @param  array   $cache_id
                 * @param  boolean $display
                 * @return string  $cache
                 */
                
            private function get_cache($file$cache_id = array(), $display TRUE) {

                    
            $cache_file $this->cache_dir.'/'.$this->generate_cache_name($file$cache_id);

                    
            // is caching enabled and does a cached file exist
                    
            if ($this->caching === TRUE AND file_exists($cache_file) AND is_readable($cache_file)) {
                        
            $cache = @file_get_contents($cache_file);

                        if (
            $cache !== FALSE) {
                            
            $cache unserialize($cache);

                            
            // the cache file is too old
                            
            if ($cache !== FALSE AND
                                
            is_array($cache) AND
                                
            count($cache) == AND
                                
            $cache[0] != AND
                                
            $cache[0] < $this->time)
                            {
                                
            $this->delete_cache_file($file$cache_id);
                            } else {

                                
            // display the cache
                                
            if ($display === TRUE) {
                                    echo 
            $cache[1];

                                
            // return the cache
                                
            } else {
                                    return 
            $cache[1];
                                }

                            }

                        }

                    }

                }



                
            /**
                 * writes a cache file
                 *
                 * @param  string $file
                 * @param  array  $cache_id
                 * @param  string $cache
                 * @return void
                 */
                
            private function write_cache($file$cache_id = array(), $cache) {

                    
            $cache_file $this->cache_dir.'/'.$this->generate_cache_name($file$cache_id);
                    
            $data serialize(array(
                        
            => $this->time $this->cache_lifetime,
                        
            => $cache
                    
            ));

                    if (!
            file_put_contents($cache_file$dataLOCK_EX)) {
                        
            trigger_error('Template: Cache file could not be written!'E_USER_ERROR);
                    }

                }



                
            /**
                 * generates the name for a cache file
                 *
                 * @param  string $file
                 * @param  array  $cache_id
                 * @return string $name
                 */
                
            private function generate_cache_name($file$cache_id = array()) {
                    if (
            count($cache_id) == 0) {
                        return 
            md5($file).$this->cache_extension;
                    } else {
                        
            $seperator_in_cache_id FALSE;
                        foreach (
            $cache_id as $value) {
                            if (
            strpos($value'|') !== FALSE) {
                                
            $seperator_in_cache_id TRUE;
                            }
                        }

                        if (
            $seperator_in_cache_id === TRUE) {
                            
            trigger_error('Template: Seperator "|" is not allowed in $cache_id!'E_USER_ERROR);
                        }

                        return 
            md5($file.'|'.implode('|'$cache_id)).$this->cache_extension;
                    }
                }


            }

            Kommentar

            Lädt...
            X