What's new? | Help | Directory | Sign in
Google
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

/**
* Joss framework & content management system.
*
* Inspired by Brian Lozier's <brian@massassi.net> Template Engines
* at http://massassi.com/php/articles/template_engines/. Created 30.1.2008.
*
* @author Jan (Honza) Javorek aka Littlemaple http://www.javorek.net
* @copyright Copyright (c) 2008 Jan Javorek
* @package Joss
* @link http://code.google.com/p/joss-cms/
* @license GNU GENERAL PUBLIC LICENSE version 2
*/



/**
* Template engine.
*
* @author Jan (Honza) Javorek aka Littlemaple http://www.javorek.net
* @copyright Copyright (c) 2008 Jan Javorek
* @package Joss
* @version $Revision$ ($Date$, $Author$)
*/
class JTemplate extends Object {

/**
* Template file.
*
* @var JFile
*/
protected $tpl;

/**
* Holds all the template variables.
*
* @var array
*/
protected $vars = array();

/**
* Template.
*
* @param $file string The file you want to load.
*/
function __construct($file) {
$f = new JFile($file);
if (!$f->exists()) {
throw new FileNotFoundException("File '$file' does not exist.");
} else {
$this->tpl = $f;
}
}

/**
* Set a template variable.
*
* @param string $name
* @param mixed $value
* @param bool $escape Explicit setting of escaping.
*/
public function set($name, $value, $escape = TRUE) {
if (!is_object($value) && $escape) { // variable, should be escaped
if (is_array($value)) {
array_walk_recursive($value, array($this, 'escape'));
$this->vars[$name] = $value;
} else {
$this->vars[$name] = $this->escape($value);
}
} else { // variable, should NOT be escaped
if ($value instanceof JTemplate) {
$value = $value->fetch(); // embedded template
}
$this->vars[$name] = $value;
}
}

/**
* Escaping of variables.
*
* @param mixed $str unescaped string
* @return mixed escaped string
*/
private function escape($str) {
return (is_string($str))? htmlspecialchars($str) : $str;
}

/**
* Open, parse, and return the template file.
*/
public function fetch() {
extract($this->vars); // extract the vars to local namespace
ob_start(); // start output buffering

if (!ini_get('short_open_tag')) { // explicit short open tags support
eval('?>' . preg_replace(
array('~<\\?(\\s)~', '~<\\?=~'),
array('<?php\\1', '<?php echo'),
$this->tpl->content));
} else {
include($this->tpl->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
}

public function __toString() {
return $this->fetch();
}

}
Show details Hide details

Change log

r84 by jan.javorek on Jul 02, 2008   Diff
Added forms from Nette. Removed JInput
from all Joss because of an
incompatibility with forms and thanks to
my smaller paranoia. Added exceptions.php
and interfaces.php. Changes in document
handling, again reorganized JDoc. Nearly
completed core of drivers. Actions removed
-- they arn't necessary at this moment.
Other minor changes.
Go to: 
Project members, sign in to write a code review

Older revisions

r78 by jan.javorek on Jun 29, 2008   Diff
Completely changed system of
exceptions. Used exceptions from
Nette. Reorganized /class/ directory.
Integrated latest files from Nette.
r36 by jan.javorek on Jun 09, 2008   Diff
Language support.
r17 by jan.javorek on Feb 02, 2008   Diff
Initial SVN settings.
All revisions of this file

File info

Size: 2955 bytes, 114 lines

File properties

svn:keywords
Date Revision Author HeadURL Id