-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 88e813a
Showing
5 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
![LightView](./docs/header.png) | ||
|
||
# LightView | ||
|
||
A Fast, Easy and Lightweight Template Engine for Web Applications. [Based on article by David Adams](https://codeshack.io/lightweight-template-engine-php/). The unique feature added is concept of computed output. | ||
## Features | ||
|
||
* Fast | ||
* Easy | ||
* Lightweight | ||
* Supports Layout | ||
* Supports Computed output | ||
|
||
|
||
## Installtion | ||
``` | ||
composer require nabeelalihashmi/LightView | ||
``` | ||
|
||
## Basic Usage | ||
|
||
Configure your application's views folder and cache folder | ||
|
||
``` | ||
LightView::$cache_path = '../cache'; | ||
LightView::$views_path = '../app/views'; | ||
``` | ||
|
||
Render the view like | ||
|
||
``` | ||
LightView::render('myView.html', ['message' => 'Hi']); | ||
``` | ||
|
||
A view can be: | ||
``` | ||
<!-- layout.html --> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{% yield title %}</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
{% include partials/nav.html %} | ||
{% yield content %} | ||
</body> | ||
</html> | ||
<!-- myView.html --> | ||
{% extends layouts/main.html %} | ||
{% block title %}Home Page{% endblock %} | ||
{% block content %} | ||
<h1>Home</h1> | ||
<p>Welcome to the home page!</p> | ||
<p> | ||
We Have A Message For You | ||
{{ $message }} | ||
{% | ||
echo 'hi'; | ||
%} | ||
</p> | ||
{% endblock %} | ||
``` | ||
|
||
## Layout | ||
Layout can be simple file having multiple yields and includes. | ||
|
||
|
||
## PHP Code | ||
You can use php code inside `{% %}` block | ||
|
||
``` | ||
{% echo 'hi'; %} | ||
{% foreach($messages as $message): %} | ||
<li> {{ $msg }} </li> | ||
{% endforeach %} | ||
``` | ||
------------------------- | ||
|
||
## Computed Block | ||
|
||
This outputs the html output of code instead of echoing. Good for saving resources. | ||
``` | ||
{( Namespace\Class,method,arg1,arg2 )} | ||
``` | ||
## Print Escaped | ||
|
||
``` | ||
{{ $variable }} | ||
``` | ||
|
||
## Print Unescape | ||
|
||
``` | ||
{! $variable !} | ||
``` | ||
## License | ||
|
||
LightView is released under permissive licese with following conditions: | ||
|
||
* It cannot be used to create adult apps. | ||
* It cannot be used to gambling apps. | ||
* It cannot be used to create apps having hate speech. | ||
|
||
### MIT License | ||
|
||
Copyright 2022 Nabeel Ali | IconicCodes.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "nabeelalihashmi/lightview", | ||
"description": "Lightweight template engine", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"IconicCodes\\LightView\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Nabeel Ali", | ||
"email": "mail2nabeelali@gmail.com" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": {} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace IconicCodes\LightView; | ||
|
||
class LightView { | ||
|
||
static $blocks = array(); | ||
public static $cache_path = 'cache/'; | ||
public static $cache_enabled = false; | ||
public static $views_path; | ||
|
||
|
||
static function view($file, $data = array()) { | ||
$cached_file = self::cache($file); | ||
extract($data, EXTR_SKIP); | ||
include $cached_file; | ||
} | ||
|
||
static function cache($file) { | ||
if (!file_exists(self::$cache_path)) { | ||
mkdir(self::$cache_path, 0744); | ||
} | ||
$cached_file = self::$cache_path . str_replace(array('/', '.html'), array('_', ''), $file . '.php'); | ||
if (!self::$cache_enabled || !file_exists($cached_file) || filemtime($cached_file) < filemtime(self::$views_path .$file)) { | ||
$code = self::includeFiles($file); | ||
$code = self::compileCode($code); | ||
file_put_contents($cached_file, '<?php class_exists(\'' . __CLASS__ . '\') or exit; ?>' . PHP_EOL . $code); | ||
} | ||
return $cached_file; | ||
} | ||
|
||
static function clearCache() { | ||
foreach(glob(self::$cache_path . '*') as $file) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
static function compileCode($code) { | ||
$code = self::compileBlock($code); | ||
$code = self::compileYield($code); | ||
$code = self::compileEscapedEchos($code); | ||
$code = self::compileEchos($code); | ||
$code = self::compilePHP($code); | ||
$code = self::compileReplaceCodeBlack($code); | ||
return $code; | ||
} | ||
|
||
static function includeFiles($file) { | ||
$filename = self::$views_path . '/' . $file; | ||
$code = file_get_contents(self::$views_path . $file); | ||
preg_match_all('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', $code, $matches, PREG_SET_ORDER); | ||
foreach ($matches as $value) { | ||
$code = str_replace($value[0], self::includeFiles($value[2]), $code); | ||
} | ||
$code = preg_replace('/{% ?(extends|include) ?\'?(.*?)\'? ?%}/i', '', $code); | ||
return $code; | ||
} | ||
|
||
static function compilePHP($code) { | ||
return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code); | ||
} | ||
|
||
|
||
static function compileEchos($code) { | ||
return preg_replace('~\{!\s*(.+?)\s*\!}~is', '<?php echo $1 ?>', $code); | ||
} | ||
|
||
static function compileEscapedEchos($code) { | ||
return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code); | ||
} | ||
|
||
static function compileBlock($code) { | ||
preg_match_all('/{% ?block ?(.*?) ?%}(.*?){% ?endblock ?%}/is', $code, $matches, PREG_SET_ORDER); | ||
foreach ($matches as $value) { | ||
if (!array_key_exists($value[1], self::$blocks)) self::$blocks[$value[1]] = ''; | ||
if (strpos($value[2], '@parent') === false) { | ||
self::$blocks[$value[1]] = $value[2]; | ||
} else { | ||
self::$blocks[$value[1]] = str_replace('@parent', self::$blocks[$value[1]], $value[2]); | ||
} | ||
$code = str_replace($value[0], '', $code); | ||
} | ||
return $code; | ||
} | ||
|
||
static function compileReplaceCodeBlack($code) { | ||
preg_match_all('/{\( (.*?) \)}/is', $code, $matches, PREG_SET_ORDER); | ||
foreach($matches as $value) { | ||
$staticFunction = explode( ',', $value[1]); | ||
$data = call_user_func_array([$staticFunction[0], $staticFunction[1]], array_slice($staticFunction, 2)); | ||
$code = str_replace($value[0], $data, $code); | ||
} | ||
|
||
return $code; | ||
|
||
} | ||
static function compileYield($code) { | ||
foreach(self::$blocks as $block => $value) { | ||
$code = preg_replace('/{% ?yield ?' . $block . ' ?%}/', $value, $code); | ||
} | ||
$code = preg_replace('/{% ?yield ?(.*?) ?%}/i', '', $code); | ||
return $code; | ||
} | ||
} |