├── README.md
├── index.php
├── template.php
└── tpl.html
/README.md:
--------------------------------------------------------------------------------
1 | # php-template
2 |
3 | Simple PHP template class.
4 |
5 | ## Create template file
6 | [Example](https://github.com/isRuslan/php-template/blob/master/tpl.html)
7 | Template variables:
8 | - `{var}` - single variable
9 | - `{loop items} {name} {end loop}` - loop for items array
10 | - `{%index%}` - index of item in items loop
11 |
12 | ## Create index.php file
13 | [Example](https://github.com/isRuslan/php-template/blob/master/index.php)
14 | - Include template class:
15 | `include 'template.php';`
16 | - Create an object:
17 | `$template = new Template();`
18 | - Set template variables, single:
19 | `$template->assign( 'my_var', 'test' );`
20 | - Set template variables, loops:
21 | `$template->assign( 'items', array( array( 'name' => 'First' ), array( 'name' => 'Second' ) ) );`
22 | - Parse template:
23 | `$template->parse( 'tpl.html' );`
24 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | assign('my_var', 'test');
7 |
8 | $template->assign('items', array(
9 | array('name' => 'First'),
10 | array('name' => 'Second')
11 | ));
12 |
13 | $template->parse('tpl.html');
14 |
--------------------------------------------------------------------------------
/template.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | class Template {
8 | /**
9 | * Content variables
10 | * @access private
11 | * @var array
12 | */
13 | private $vars = array();
14 |
15 | /**
16 | * Content delimiters
17 | * @access private
18 | * @var string
19 | */
20 | private $l_delim = '{',
21 | $r_delim = '}';
22 |
23 | /**
24 | * Set template property in template file
25 | * @access public
26 | * @param string $key property name
27 | * @param string $value property value
28 | */
29 | public function assign( $key, $value ) {
30 | $this->vars[$key] = $value;
31 | }
32 |
33 | /**
34 | * Parce template file
35 | * @access public
36 | * @param string $template_file
37 | */
38 | public function parse( $template_file ) {
39 | if ( file_exists( $template_file ) ) {
40 | $content = file_get_contents($template_file);
41 |
42 | foreach ( $this->vars as $key => $value ) {
43 | if ( is_array( $value ) ) {
44 | $content = $this->parsePair($key, $value, $content);
45 | } else {
46 | $content = $this->parseSingle($key, (string) $value, $content);
47 | }
48 | }
49 |
50 | eval( '?> ' . $content . 'Template error' );
53 | }
54 | }
55 |
56 | /**
57 | * Parsing content for single varliable
58 | * @access private
59 | * @param string $key property name
60 | * @param string $value property value
61 | * @param string $string content to replace
62 | * @param integer $index index of loop item
63 | * @return string replaced content
64 | */
65 | private function parseSingle( $key, $value, $string, $index = null ) {
66 | if ( isset( $index ) ) {
67 | $string = str_replace( $this->l_delim . '%index%' . $this->r_delim, $index, $string );
68 | }
69 | return str_replace( $this->l_delim . $key . $this->r_delim, $value, $string );
70 | }
71 |
72 | /**
73 | * Parsing content for loop varliable
74 | * @access private
75 | * @param string $variable loop name
76 | * @param string $value loop data
77 | * @param string $string content to replace
78 | * @return string replaced content
79 | */
80 | private function parsePair( $variable, $data, $string ) {
81 | $match = $this->matchPair($string, $variable);
82 | if( $match == false ) return $string;
83 |
84 | $str = '';
85 | foreach ( $data as $k_row => $row ) {
86 | $temp = $match['1'];
87 | foreach( $row as $key => $val ) {
88 | if( !is_array( $val ) ) {
89 | $index = array_search( $k_row, array_keys( $data ) );
90 | $temp = $this->parseSingle( $key, $val, $temp, $index );
91 | } else {
92 | $temp = $this->parsePair( $key, $val, $temp );
93 | }
94 | }
95 | $str .= $temp;
96 | }
97 |
98 | return str_replace( $match['0'], $str, $string );
99 | }
100 |
101 | /**
102 | * Match loop pair
103 | * @access private
104 | * @param string $string content with loop
105 | * @param string $variable loop name
106 | * @return string matched content
107 | */
108 | private function matchPair( $string, $variable ) {
109 | if ( !preg_match("|" . preg_quote($this->l_delim) . 'loop ' . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . 'end loop' . preg_quote($this->r_delim) . "|s", $string, $match ) ) {
110 | return false;
111 | }
112 |
113 | return $match;
114 | }
115 | }
116 |
117 |
--------------------------------------------------------------------------------
/tpl.html:
--------------------------------------------------------------------------------
1 |