Skip to content

Commit 29b1c89

Browse files
committed
Simples Template package
0 parents  commit 29b1c89

File tree

4 files changed

+354
-0
lines changed

4 files changed

+354
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Grupo de PHP da Zona da Mata
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "phpzm/template",
3+
"description": "Simples Template package",
4+
"minimum-stability": "dev",
5+
"keywords": [
6+
"php",
7+
"framework",
8+
"api",
9+
"simples"
10+
],
11+
"homepage": "https://github.com/phpzm/template",
12+
"license": "MIT",
13+
"version": "1.0.0",
14+
"type": "package",
15+
"authors": [
16+
{
17+
"name": "William",
18+
"email": "wilcorrea@gmail.com"
19+
},
20+
{
21+
"name": "Ezio",
22+
"email": "ezioadsr@gmail.com"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.0",
27+
"phpzm/http": "dev-master"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Simples\\Template\\": "src/"
32+
}
33+
}
34+
}

src/Tools.php

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace Simples\Template;
4+
5+
use Simples\Http\Kernel\App;
6+
7+
/**
8+
* Class Tools
9+
* @package Simples\Template
10+
*/
11+
class Tools
12+
{
13+
/**
14+
* @var mixed
15+
*/
16+
protected $data;
17+
18+
/**
19+
* @SuppressWarnings("BooleanArgumentFlag")
20+
*
21+
* @param string $path
22+
* @param bool $print
23+
* @return string
24+
*/
25+
protected function href(string $path, $print = true)
26+
{
27+
$route = App::route($path);
28+
if ($print) {
29+
out($route);
30+
}
31+
return $route;
32+
}
33+
34+
/**
35+
* @SuppressWarnings("BooleanArgumentFlag")
36+
*
37+
* @param bool $print
38+
* @return string
39+
*/
40+
public function here($print = true)
41+
{
42+
return $this::href($this->uri(), $print);
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function uri()
49+
{
50+
return substr(App::request()->getUri(), 0, -1);
51+
}
52+
53+
/**
54+
* @param $href
55+
* @return bool
56+
*/
57+
public function match($href)
58+
{
59+
return strpos(App::request()->getUri(), $href) === 0;
60+
}
61+
62+
/**
63+
* @SuppressWarnings("BooleanArgumentFlag")
64+
*
65+
* @param string $path
66+
* @param bool $print
67+
* @return string
68+
*/
69+
public function image($path, $print = true)
70+
{
71+
return $this->asset('images/' . $this->fix($path), $print);
72+
}
73+
74+
/**
75+
* @SuppressWarnings("BooleanArgumentFlag")
76+
*
77+
* @param string $path
78+
* @param bool $print
79+
* @return string
80+
*/
81+
public function style($path, $print = true)
82+
{
83+
return $this->asset('styles/' . $this->fix($path), $print);
84+
}
85+
86+
/**
87+
* @SuppressWarnings("BooleanArgumentFlag")
88+
*
89+
* @param string $path
90+
* @param bool $print
91+
* @return string
92+
*/
93+
public function script($path, $print = true)
94+
{
95+
return $this->asset('scripts/' . $this->fix($path), $print);
96+
}
97+
98+
/**
99+
* @SuppressWarnings("BooleanArgumentFlag")
100+
*
101+
* @param $path
102+
* @param bool $print
103+
* @return string
104+
*/
105+
public function asset($path, $print = true)
106+
{
107+
return $this->href('assets/' . $this->fix($path) . $this->test(), $print);
108+
}
109+
110+
/**
111+
* @param string $path
112+
* @return string
113+
*/
114+
private function fix($path)
115+
{
116+
return (gettype($path) === TYPE_STRING && $path{0} === '/') ? substr($path, 1) : $path;
117+
}
118+
119+
/**
120+
* @param $value
121+
* @param $index
122+
* @return string
123+
*/
124+
public function stamp($value, $index = null)
125+
{
126+
if (is_null($index)) {
127+
return out($value);
128+
}
129+
return $this->off($value, $index);
130+
}
131+
132+
/**
133+
* @param $value
134+
* @param $index
135+
* @return string
136+
*/
137+
public function off($value, $index)
138+
{
139+
return out(off($value, $index));
140+
}
141+
142+
/**
143+
* @param $index
144+
* @return string
145+
*/
146+
public function out($index)
147+
{
148+
return out($this->get($index));
149+
}
150+
151+
/**
152+
* @param $index
153+
* @param $default
154+
* @return mixed
155+
*/
156+
protected function get($index = null, $default = null)
157+
{
158+
if (is_null($index)) {
159+
return $this->data;
160+
}
161+
return off($this->data, $index, $default);
162+
}
163+
164+
/**
165+
* @return string
166+
*/
167+
private function test()
168+
{
169+
return env('TEST_MODE') ? '?c=' . uniqid() : '';
170+
}
171+
}

src/View.php

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Simples\Template;
4+
5+
/**
6+
* Class View
7+
* @package Simples\Template
8+
*/
9+
class View extends Tools
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $root;
15+
16+
/**
17+
* @var object
18+
*/
19+
private $layout;
20+
21+
/**
22+
* @var array
23+
*/
24+
private $sections;
25+
26+
/**
27+
* Engine constructor.
28+
* @param $root
29+
*/
30+
public function __construct($root)
31+
{
32+
$this->root = $root;
33+
}
34+
35+
/**
36+
* @param $template
37+
* @param $data
38+
* @return string
39+
*/
40+
public function render($template, $data)
41+
{
42+
$content = $this->compile($template, $data);
43+
44+
while ($this->layout) {
45+
$layout = $this->layout;
46+
47+
$this->sections[$layout->section] = $content;
48+
49+
$this->layout = null;
50+
51+
$content = $this->compile($layout->template, array_merge($layout->data, $this->data));
52+
}
53+
54+
return $content;
55+
}
56+
57+
/**
58+
* @param $template
59+
* @param $data
60+
* @return string
61+
*/
62+
private function compile($template, $data)
63+
{
64+
$filename = path($this->root, $template);
65+
66+
$this->data = $data;
67+
68+
ob_start();
69+
if (file_exists($filename)) {
70+
if (!is_array($data)) {
71+
$data = [$data];
72+
}
73+
extract($data);
74+
75+
/** @noinspection PhpIncludeInspection */
76+
$callable = include $filename;
77+
78+
if (is_callable($callable)) {
79+
call_user_func_array($callable, array_values($data));
80+
}
81+
}
82+
$content = ob_get_contents();
83+
84+
ob_end_clean();
85+
86+
return $content;
87+
}
88+
89+
/**
90+
* @param $layout
91+
* @param $section
92+
* @param array $data
93+
*/
94+
protected function extend($layout, $section, array $data = [])
95+
{
96+
$layout = (object)['section' => $section, 'template' => $layout, 'data' => $data];
97+
98+
$this->layout = $layout;
99+
}
100+
101+
/**
102+
* @SuppressWarnings("BooleanArgumentFlag")
103+
*
104+
* @param $name
105+
* @param bool $print
106+
* @return string
107+
*/
108+
protected function grant($name, $print = true)
109+
{
110+
$section = off($this->sections, $name);
111+
if ($print) {
112+
out($section);
113+
}
114+
return $section;
115+
}
116+
117+
/**
118+
* @param $template
119+
* @return mixed
120+
*/
121+
protected function append($template)
122+
{
123+
$filename = path($this->root, $template);
124+
125+
/** @noinspection PhpIncludeInspection */
126+
return include $filename;
127+
}
128+
}

0 commit comments

Comments
 (0)