php.de

Zurück   php.de > Webentwicklung > PHP-Fortgeschrittene

PHP-Fortgeschrittene Arbeiten mit PHP ohne Einschränkungen

Antwort
 
LinkBack Themen-Optionen Thema bewerten
Alt 23.09.2005, 10:43  
Gast
 
Beiträge: n/a
Standard [Erledigt] Templates

Hi!

Ich bin gerade dabei meine Seite neu zu programmieren und will diesmal nicht wieder so ein durcheinander haben. Templates wären da ja schonmal der richtige Ansatz... allerdings möchte ich nicht irgendwelche vorgefertigten Dinger eir Smarty nehmen, sondern den Mist selber schreiben!

Kennt jemand ne Anleitung zu dem Thema oder kann mir so weiterhelfen?

MfG TWron
  Mit Zitat antworten
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 23.09.2005, 10:56  
Gast
 
Beiträge: n/a
Standard

Hmm so viel gehört doch eigentlich nicht dazu. Ihc hab mir eine eigenen Klasse dafür geschrieben, die im prinzip nur eine Handvoll funktionen benötigt:

str_replace(), file(), implode(), empty() und strstr()

Mehr als das habe ich wirklich nicht gebruacht um mir meine eigenes kleines Template System zu bauen.
  Mit Zitat antworten
Alt 23.09.2005, 11:04  
Gast
 
Beiträge: n/a
Standard

ich hab da auch schon nen bissl was hinbekommen.... nur das problem ist, dass ich nicht mehrere templates zur gleichen zeit verwalten kann.

wenn ich die index.tpl ausgelesen habe, kann ich die news.tpl nicht mehr lesen...
außerdem kann ich nur durch strings ersetzen.... ziel ist aber, dass ich zb in der index.tpl die designvariable MAIN durch includes ersetze.
  Mit Zitat antworten
Alt 23.09.2005, 11:53  
Gast
 
Beiträge: n/a
Standard

such mal nach phptemplate, da gibts irgendwo eine klasse, die hat nur 20 zeilen oder so, die is sehr aufschlussreich, und finger weg von str_replace, das is SAULANGSAM, viele templatesysteme arbeiten damit, obwohl es längst überholt ist.
  Mit Zitat antworten
Alt 23.09.2005, 11:56  
Gast
 
Beiträge: n/a
Standard

seit php5 salonfähig ist benutze ich xslt...
Feine Sach dat.
  Mit Zitat antworten
Alt 23.09.2005, 11:57  
Gast
 
Beiträge: n/a
Standard

warum nicht str_replace.... das is schneller als ereg(i)_replace

was soll ich denn besser nehmen?
  Mit Zitat antworten
Alt 23.09.2005, 12:01  
Gast
 
Beiträge: n/a
Standard

das beste ist es das template folgendermaßen aufzubauen, dann ist ein str_replace nicht nötig:

PHP-Code:
<html>
<body>
blah blub
<?=$variable1?>
blubidibla
<?=$variable2?>
</body>
</html>
wozu rechenzeit mit str_replaces verschwenden, nur damit das <?=$var?> mit [var] oä ersetzt wird...
  Mit Zitat antworten
Alt 23.09.2005, 12:04  
Gast
 
Beiträge: n/a
Standard

das verfehlt aber den sinn...
1. will php und html komplett trennen und
2. soll der designer die templates ändern können

hab jetzt mal das hier gefunden.... was haltet ihr davon?

http://www.inphpfriend.de/forum/showthread.php?t=124
  Mit Zitat antworten
Alt 23.09.2005, 12:08  
Gast
 
Beiträge: n/a
Standard

das hat sehr wohl sinn, glaub mir ein str_replace is langsamer.. der designer wird wohl noch <?=$var?> statt [var] schreiben können. ich zeig dir mal die klasse dazu:

PHP-Code:
<?php


/* template.php
*
*    *---------*
*    |  USAGE  |
*    *---------*

*   -| Cached Template |-
*
*   $tpl = & new CachedTemplate($cache_name);
*   if(!($tpl->is_cached())) {
*      $tpl->set('title', 'My Title');
*      $tpl->set('list', array('cat', 'dog', 'mouse'));
*   }
*   echo $tpl->fetch_cache($templatefile);

*
*   -| normal Template |-
*
*   $tpl = & new Template();
*      $tpl->set('title', 'My Title');
*      $tpl->set('list', array('cat', 'dog', 'mouse'));
*   echo $tpl->fetch($templatefile);
*
*/

class Template {
    var 
$vars/// Holds all the template variables

    /**
     * Constructor
     *
     * @param $file string the file name you want to load
     */
    
function Template($file null) {
        
$this->file $file;
    }

    
/**
     * Set a template variable.
     */
    
function set($name$value) {
        
$this->vars[$name] = is_object($value) ? $value->fetch() : $value;
    }

    
/**
     * Open, parse, and return the template file.
     *
     * @param $file string the template file name
     */
    
function fetch($file null) {
        if(!
$file$file $this->file;

        
extract($this->vars);          // Extract the vars to local namespace
        
ob_start();                    // Start output buffering
        
include($file);                // Include the file
        
$contents ob_get_contents(); // Get the contents of the buffer
        
ob_end_clean();                // End buffering and discard
        
return $contents;              // Return the contents
    
}
}

/**
* An extension to Template that provides automatic caching of
* template contents.
*/
class CachedTemplate extends Template {
    var 
$cache_id;
    var 
$expire;
    var 
$cached;

    
/**
     * Constructor.
     *
     * @param $cache_id string unique cache identifier
     * @param $expire int number of seconds the cache will live
     */
    
function CachedTemplate($cache_id null$expire 900) {
        
$this->Template();
        
$this->cache_id $cache_id 'cache/' md5($cache_id) : $cache_id;
        
$this->expire   $expire;
    }

    
/**
     * Test to see whether the currently loaded cache_id has a valid
     * corrosponding cache file.
     */
    
function is_cached() {
        if(
$this->cached) return true;

        
// Passed a cache_id?
        
if(!$this->cache_id) return false;

        
// Cache file exists?
        
if(!file_exists($this->cache_id)) return false;

        
// Can get the time of the file?
        
if(!($mtime filemtime($this->cache_id))) return false;

        
// Cache expired?
        
if(($mtime $this->expire) < time()) {
            @
unlink($this->cache_id);
            return 
false;
        }
        else {
            
/**
             * Cache the results of this is_cached() call.  Why?  So
             * we don't have to double the overhead for each template.
             * If we didn't cache, it would be hitting the file system
             * twice as much (file_exists() & filemtime() [twice each]).
             */
            
$this->cached true;
            return 
true;
        }
    }

    
/**
     * This function returns a cached copy of a template (if it exists),
     * otherwise, it parses it as normal and caches the content.
     *
     * @param $file string the template file
     */
    
function fetch_cache($file) {
        if(
$this->is_cached()) {
            
$fp = @fopen($this->cache_id'r');
            
$contents fread($fpfilesize($this->cache_id));
            
fclose($fp);
            return 
$contents;
        }
        else {
            
$contents $this->fetch($file);

            
// Write the cache
            
if($fp = @fopen($this->cache_id'w')) {
                
fwrite($fp$contents);
                
fclose($fp);
            }
            else {
                die(
'Unable to write cache.');
            }

            return 
$contents;
        }
    }
}



?>
die is jetz durch die comments und den cache etwas aufgebläht aber in der grundfunktion grad mal 10 zeilen oder so
  Mit Zitat antworten
Alt 23.09.2005, 13:13  
Gast
 
Beiträge: n/a
Standard

aber wie bekomm ich jetzt die verf*$%?en includes darein? ^^
  Mit Zitat antworten
Antwort


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
templates und Co Elta PHP Tipps 2008 6 14.07.2008 15:13
if abfragen in Templates rob1011 PHP Tipps 2006 13 03.08.2006 15:55
Vererbung bei Templates Pain-maker PHP-Fortgeschrittene 9 28.03.2006 10:05
Templates - Variablen $tpl['dies']['und']['das'] Alpha Centauri PHP Tipps 2006 12 08.03.2006 14:39
E-Mail Templates Mano PHP Tipps 2005-2 4 09.10.2005 14:52
Templates - DB oder File? PHP Tipps 2005-2 11 23.07.2005 05:30
Templates per ACP PHP Tipps 2005-2 0 26.06.2005 20:00
[Erledigt] Templates PHP Tipps 2005 4 06.04.2005 16:24
Templates GrU3nL!nG PHP Tipps 2005 6 26.03.2005 14:47
Mit Templates Navigation erstellen? (welches Templatesyste?) zwelch PHP Tipps 2004-2 5 18.11.2004 15:14
[Erledigt] Wieder mal Templates! PHP-Fortgeschrittene 1 10.09.2004 11:42
Problem bei HP basierend auf Templates PHP Tipps 2004 3 17.07.2004 12:37
Templates PHP Tipps 2004 5 29.06.2004 16:30
Problem mit Templates suter PHP Tipps 2004 3 23.06.2004 14:40
Wie benutze ich Templates? PHP Tipps 2004 5 11.06.2004 13:23


Alle Zeitangaben in WEZ +2. Es ist jetzt 20:42 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.