Skip to content

Commit

Permalink
Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
joanmon committed Aug 21, 2015
1 parent 9824aea commit 70b5210
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 19 deletions.
151 changes: 151 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
Lightweight PHP template engine
===============================

Features
--------
* Fast

* Powerful

* Easy

Installing
----------

@todo

Getting started
---------------

* Simple template:
```
/base/path/|
|_ simple-template.phtml
|_ script.php
```
```
<!-- simple-template.phtml -->
<!DOCTYPE html>
<html>
<head>
<title><?= \jmon\TplEngine\View::get('title') ?></title>
</head>
<body>
<h1><?= \jmon\TplEngine\View::get('title') ?></h1>
</body>
</html>
```
```
<?php
// script.php
use jmon\TplEngine\View;
View::setBasePath('/base/path');
View::set('title', 'hello world!');
echo View::render('simple-template.phtml');
?>
```
```
<!-- final rendering -->
<!DOCTYPE html>
<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1>hello world!</h1>
</body>
</html>
```
* Template with layout:
```
/base/path/|
|_ layout.phtml
|_ home/index.phtml
|_ script.php
```
```
<!-- layout.phtml -->
<!DOCTYPE html>
<html>
<head>
<title><?= \jmon\TplEngine\View::get('title') ?></title>
</head>
<body>
<!-- Place where will be rendered the content of templates that extends this one -->
<?= \jmon\TplEngine\View::content() ?>
</body>
</html>
```
```
<!-- home/index.phtml -->
<?php \jmon\TplEngine\View::templateExtend('layout.phtml')?>
<h1><?= \jmon\TplEngine\View::get('title') ?></h1>
```
```
<?php
// script.php
use jmon\TplEngine\View;
View::setBasePath('/base/path');
View::set('title', 'hello world!');
echo View::render('/home/index.phtml');
?>
```
```
<!-- final rendering -->
<!DOCTYPE html>
<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1>hello world!</h1>
</body>
</html>
```
* Use of partial:
```
/base/path/|
|_ template.phtml
|_ partial.phtml
|_ script.php
```
```
<!-- template.phtml -->
<!DOCTYPE html>
<html>
<head>
<title><?= \jmon\TplEngine\View::get('title') ?></title>
</head>
<body>
<h1><?= \jmon\TplEngine\View::get('title') ?></h1>
<?php \jmon\TplEngine\View::partial('partial.phtml')?>
</body>
</html>
```
```
<!-- partial.phtml -->
<footer><?= \jmon\TplEngine\View::get('footer-text') ?></footer>
```
```
<?php
// script.php
use jmon\TplEngine\View;
View::setBasePath('/base/path');
View::set('title', 'hello world!');
View::set('footer-text', 'Awesome footer!');
echo View::render('template.phtml');
?>
```
```
<!-- final rendering -->
<!DOCTYPE html>
<html>
<head>
<title>hello world!</title>
</head>
<body>
<h1>hello world!</h1>
<footer>Awesome footer!</footer>
</body>
</html>
```
24 changes: 9 additions & 15 deletions src/TplEngine/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static function render($template)
self::$contentToRender = $templateContent;
$templateContent = self::requireIntoString(array_pop(self::$templateHeap));
}

return $templateContent;
}

Expand All @@ -65,32 +66,25 @@ public static function content()

/**
* It saves variable into template engine.
* @param string $name
*
*@param string $key
* @param string|int $value
*/
public static function set($name, $value)
public static function set($key, $value)
{
self::$vars[$name] = $value;
self::$vars[$key] = $value;
}

/**
* It gets one variable saved previously.
* @param string $name
*
*@param string $key
*
* @return mixed
*/
public static function get($name)
{
return isset(self::$vars[$name]) ? self::$vars[$name] : null;
}

/**
* It prints one variable saved previously.
* @param string $name
*/
public static function printVar($name)
public static function get($key)
{
echo isset(self::$vars[$name]) ? self::$vars[$name] : '';
return isset(self::$vars[$key]) ? self::$vars[$key] : null;
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/TplEngine/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function testViewCanRenderTemplateExtendingLayout()
public function testViewCanRenderVar()
{
View::set('varname', 'varname');
$this->assertEquals('varname', View::render('testvar.phtml'));
$this->assertEquals('varname', View::render('getvar.phtml'));
}
public function testViewMultiInherit()
Expand Down
2 changes: 1 addition & 1 deletion test/TplEngine/testviews/getvar.phtml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php echo \jmon\TplEngine\View::get('varname') ?>
<?= \jmon\TplEngine\View::get('varname') ?>
2 changes: 1 addition & 1 deletion test/TplEngine/testviews/one.phtml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1<?php echo \jmon\TplEngine\View::content() ?>1
1<?= \jmon\TplEngine\View::content() ?>1
1 change: 0 additions & 1 deletion test/TplEngine/testviews/testvar.phtml

This file was deleted.

0 comments on commit 70b5210

Please sign in to comment.