Skip to content

Commit 607e826

Browse files
committed
Add base Templater class
0 parents  commit 607e826

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Temuri Takalandze
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.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Cherry-Templater
2+
3+
**2019 © Cherry-project**

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "cherry-project/templater",
3+
"description": "Cherry-project Template Engine",
4+
"type": "library",
5+
"keywords": ["cherry","templating"],
6+
"homepage": "https://github.com/ABGEO07/cherry-templater",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "ABGEO",
11+
"email": "[email protected]",
12+
"homepage": "http://www.abgeo.ga",
13+
"role": "Developer"
14+
}
15+
],
16+
"minimum-stability": "dev",
17+
"require": {
18+
"php": ">=5.6.0",
19+
"cherry-project/response": "v1.0.1"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Cherry\\": "src/"
24+
}
25+
}
26+
}

composer.lock

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Hello, World!</title>
6+
</head>
7+
<body>
8+
<h1>Hello, <?php echo $name; ?>!</h1>
9+
</body>
10+
</html>

examples/test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
//Include autoloader
4+
require_once __DIR__ . '/../vendor/autoload.php';
5+
6+
use Cherry\Templating\Templater;
7+
8+
$templateEngine = new Templater(__DIR__ . '/../examples/templates');
9+
10+
echo $templateEngine->render('index', [
11+
'name' => 'Temuri'
12+
]);

src/Templating/Templater.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* The file contains Templater class
4+
*
5+
* PHP version 5
6+
*
7+
* @category Library
8+
* @package Cherry
9+
* @author Temuri Takalandze <[email protected]>
10+
* @license https://github.com/ABGEO07/cherry-templater/blob/master/LICENSE MIT
11+
* @link https://github.com/ABGEO07/cherry-templater
12+
*/
13+
14+
namespace Cherry\Templating;
15+
16+
use Cherry\HttpUtils\Response;
17+
18+
/**
19+
* Cherry project templater class
20+
*
21+
* @category Library
22+
* @package Cherry
23+
* @author Temuri Takalandze <[email protected]>
24+
* @license https://github.com/ABGEO07/cherry-templater/blob/master/LICENSE MIT
25+
* @link https://github.com/ABGEO07/cherry-templater
26+
*/
27+
class Templater
28+
{
29+
private $_templatesPath;
30+
31+
/**
32+
* Templater constructor.
33+
*
34+
* @param string $templatesPath Path to templates.
35+
*/
36+
public function __construct($templatesPath)
37+
{
38+
$this->_templatesPath = $templatesPath;
39+
}
40+
41+
/**
42+
* Render given template and return response.
43+
*
44+
* @param string $template Template filename in templates folder.
45+
* @param array $variables Arguments for template.
46+
*
47+
* @return Response
48+
*/
49+
public function render($template, $variables = [])
50+
{
51+
if (substr($template, -13) !== '.templater.php') {
52+
$template .= '.templater.php';
53+
}
54+
55+
$templatesPath = $this->_templatesPath;
56+
57+
if (file_exists($templatesPath . '/' . $template)) {
58+
//Convert array elements into variables
59+
extract($variables, EXTR_OVERWRITE);
60+
61+
//Start Output Buffer session
62+
ob_start();
63+
64+
// Include Template File
65+
include_once "{$templatesPath}/{$template}";
66+
67+
//Get Buffer value
68+
$content = ob_get_contents();
69+
70+
//Close Buffer session and clear it
71+
ob_end_clean();
72+
} else {
73+
$content = "Template {$template} not found!";
74+
}
75+
76+
//Create new Response object
77+
$response = new Response();
78+
79+
return $response->sendResponse($content);
80+
}
81+
}

0 commit comments

Comments
 (0)