├── README.md
└── pxml.php
/README.md:
--------------------------------------------------------------------------------
1 | PXML TPL
2 | ==========
3 |
4 | A smart tiny and lightest OO php templating system .
5 | Only it replaces some words with others ( uses "str_ireplace()" ) .
6 | - No regex .
7 | - No complex code .
8 | - Very light (the lightest) .
9 | - Write php as xml/html .
10 | - Self Container for ( vars & methods ) .
11 | - Object Oriented .
12 |
13 | ***
14 |
15 | Usage
16 | =========
17 |
18 | **Config it:**
19 | ```php
20 | add(array(
30 | '(php):' => ' ' ?> '
32 | ));
33 |
34 | // render a file (and pass vars "optional")
35 | $pxml->render('path/to/file.html', array('var' => 'value'));
36 |
37 | ```
38 |
39 | **An PHTML file**
40 | ```php
41 |
$c
42 | $this->alert = 'hi'
43 | $this->alert
44 |
45 |
46 |
47 | $array = array( 1,2,3,4 );
48 | $array as $v
49 | $v
50 |
51 |
52 |
53 |
54 | $array = array( 1,2,3,4 );
55 | !empty($xxx)
56 | "HI 1"
57 | !empty($yyyy)
58 | "HI 2"
59 |
60 | "HI 3"
61 |
62 |
63 |
64 |
65 |
66 | $i=0; $i <= 5; ++$i
67 | $i
68 |
69 |
70 |
71 |
72 | $x = 4
73 | $x < 5
74 | $x, " - " , "HI"
75 | ++$x
76 |
77 | ```
78 |
--------------------------------------------------------------------------------
/pxml.php:
--------------------------------------------------------------------------------
1 | & )
11 | * @copyright 2014
12 | * @version 1.0
13 | * @license MIT
14 | * @access public
15 | */
16 | class pxml
17 | {
18 | protected $file;
19 | protected $vars = array();
20 | var $pxml = array (
21 |
22 | // php
23 | '' => ' ' => ' ?> ',
25 |
26 | // foreach
27 | '' => ' ' => ' ): ?> ',
29 | '' => ' ',
30 |
31 | // while
32 | '' => ' ' => ' ): ?> ',
34 | '' => ' ',
35 |
36 | // for
37 | '' => ' ' => ' ): ?> ',
39 | '' => ' ',
40 |
41 | // print / echo
42 | '' => ' ' => ' ; ?> ',
44 |
45 | // if,elseif,else
46 | '' => ' ' => ' ): ?> ',
48 | '' => ' ' => ' ): ?> ',
50 | '' => ' ',
51 | '' => ' ',
52 | '' => ' '
53 | );
54 |
55 | // --------------------------------------
56 |
57 | /**
58 | * Add your own replacements
59 | *
60 | * @param array $data
61 | * @return void
62 | */
63 | function add(array $data)
64 | {
65 | $this->pxml = array_merge($this->pxml, $data);
66 | }
67 |
68 | // --------------------------------------
69 |
70 | /**
71 | * Render a file
72 | *
73 | * @param string $file
74 | * @param array $vars
75 | * @return bool
76 | */
77 | function render($file, array $vars = array())
78 | {
79 | if(!is_file($file)) return FALSE;
80 |
81 | extract($vars, EXTR_SKIP);
82 | $this->file = $file;
83 | eval( '?>' . $this->compile() );
84 |
85 | return TRUE;
86 | }
87 |
88 | // --------------------------------------
89 |
90 | /**
91 | * Compile a file
92 | *
93 | * @return string
94 | */
95 | protected function compile()
96 | {
97 | $d = file_get_contents($this->file);
98 | return str_ireplace (
99 | array_keys($this->pxml),
100 | array_values($this->pxml),
101 | $d
102 | );
103 | }
104 |
105 | // --------------------------------------
106 |
107 | function __set($k, $v)
108 | {
109 | $this->vars[$k] = $v;
110 | }
111 |
112 | // --------------------------------------
113 |
114 | function __get($k)
115 | {
116 | return @$this->vars[$k];
117 | }
118 |
119 | // --------------------------------------
120 |
121 | function __unset($k)
122 | {
123 | unset($this->vars[$k]);
124 | }
125 |
126 | // --------------------------------------
127 |
128 | function __isset($k)
129 | {
130 | return isset($this->vars[$k]);
131 | }
132 |
133 | // --------------------------------------
134 |
135 | function __call($m, $a)
136 | {
137 | return call_user_func_array(array($this->vars, $m), $a);
138 | }
139 | }
140 |
--------------------------------------------------------------------------------