Announcement

Collapse
No announcement yet.

PHP Template System

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Template System

    Hallo liebe PHP'ler,

    da ich ein Template System benötige welches nicht annähernd so viele Funktionalitäten bieten muss wie z.B. Smarty, machte ich mich auf die Suche nach etwas Einfacherem.

    Auf dieser Seite ( Verweis ), wurde ich sogleich fündig und es bietet schon fast Alles, was ich benötige. Beim durchlesen viel mir nur auf, das das Template in keinerweise geparst wird, wodurch man demnach ( meines Wissens, Anfänger ) unter anderem keinen "include" setzen könnte, um evtl. News o.ä. reinzuladen.

    Da meine Kenntnisse noch nicht so ausgereift sind, diesen für euch wohl simplen Code mit einer "parse"-Funktion zu versehen ( oder auch eine andere Methode? ), erbitte ich hier eure Hilfe. Vielleicht reicht auch schon ein Gedankenstoss in die richtige Richtung.

    Lieben Gruß und Dank auch wenn ihr es nur lest
    Gizzm0


    Code:
    <?php
    
    	/**
    	 * Simple template engine class (use [@tag] tags in your templates).
    	 * 
    	 * @link http://www.broculos.net/ Broculos.net Programming Tutorials
    	 * @author Nuno Freitas <[email protected]>
    	 * @version 1.0
    	 */
        class Template {
        	/**
        	 * The filename of the template to load.
        	 *
        	 * @access protected
        	 * @var string
        	 */
            protected $file;
            
            /**
             * An array of values for replacing each tag on the template (the key for each value is its corresponding tag).
             *
             * @access protected
             * @var array
             */
            protected $values = array();
            
            /**
             * Creates a new Template object and sets its associated file.
             *
             * @param string $file the filename of the template to load
             */        
            public function __construct($file) {
                $this->file = $file;
            }
            
            /**
             * Sets a value for replacing a specific tag.
             *
             * @param string $key the name of the tag to replace
             * @param string $value the value to replace
             */
            public function set($key, $value) {
                $this->values[$key] = $value;
            }
            
            /**
             * Outputs the content of the template, replacing the keys for its respective values.
             *
             * @return string
             */
            public function output() {
            	/**
            	 * Tries to verify if the file exists.
            	 * If it doesn't return with an error message.
            	 * Anything else loads the file contents and loops through the array replacing every key for its value.
            	 */
                if (!file_exists($this->file)) {
                	return "Error loading template file ($this->file).<br />";
                }
                $output = file_get_contents($this->file);
                
                foreach ($this->values as $key => $value) {
                	$tagToReplace = "$key";
                	$output = str_replace("{".$tagToReplace."}", $value, $output);
                }
    
                return $output;
            }
            
            /**
             * Merges the content from an array of templates and separates it with $separator.
             *
             * @param array $templates an array of Template objects to merge
             * @param string $separator the string that is used between each Template object
             * @return string
             */
            static public function merge($templates, $separator = "\n") {
            	/**
            	 * Loops through the array concatenating the outputs from each template, separating with $separator.
            	 * If a type different from Template is found we provide an error message. 
            	 */
                $output = "";
                
                foreach ($templates as $template) {
                	$content = (get_class($template) !== "Template")
                		? "Error, incorrect type - expected Template."
                		: $template->output();
                	$output .= $content . $separator;
                }
                
                return $output;
            }
        }
    
    ?>
    Edit - bitte Thread löschen.

    Muss echt mit dem falschen Fuss aufgestanden sein. Der totale Denkfehler. Wieso auch nen Template machen wenn man sowieso PHP & HTML mischt.

    So sollte es laufen:
    index.php -> includes ( z.B. news.php, ect....) -> Templates

    Man, man, was hat mich da nur blockiert
    Zuletzt editiert von Gizzm0; 28.08.2011, 13:14. Reason: Was ein Quatsch xD
Working...
X