Commits

Krzysztof Rakowski committed dea3cf4

initial commit

Comments (0)

Files changed (4)

custom-class/MyView.php

+<?php
+class MyView {
+
+    protected $template_dir = 'templates/';
+    protected $vars = array();
+
+    public function __construct($template_dir = null) {
+        if ($template_dir !== null) {
+            // you should check here if this dir really exists
+            $this->template_dir = $template_dir;
+        }
+    }
+    
+    public function render($template_file) {
+        if (file_exists($this->template_dir.$template_file)) {
+            include $this->template_dir.$template_file;
+        } else {
+            throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir);
+        }
+    }
+    
+    public function __set($name, $value) {
+        $this->vars[$name] = $value;
+    }
+    
+    public function __get($name) {
+        return $this->vars[$name];
+    }
+
+}
+
+?>

custom-class/index.php

+<?php
+
+include_once('MyView.php');
+
+$t = new MyView();
+
+$t->friends = array(
+    'Rachel', 'Monica', 'Phoebe', 'Chandler', 'Joey', 'Ross'
+);
+
+$t->render('index.phtml');
+$t->render('index.xml');
+
+
+?>

custom-class/templates/index.phtml

+<html>
+<body>
+Names of my friends:
+<ul>
+    <?php foreach ($this->friends as $friend): ?>
+        <li><?=$friend?></li>
+    <?php endforeach; ?>
+</ul>
+</body>
+</html>

custom-class/templates/index.xml

+<?='<?xml version="1.0" encoding="utf-8"?>'?>
+<myfriends>
+    <?php foreach ($this->friends as $friend): ?>
+        <friend><?=$friend?></friend>
+    <?php endforeach; ?>
+</myfriends>