Tangerine is a project to introduce myself with the concept of templating engine.
- Php8.0
- PhpAutoloader (Stable Release)
- Clone the repo
$> git clone https://lilianQ-Q/Tangerine.git
- Configure your autoloader
{ "Tangerine\\" : "path/to/tangerine/src/folder" }
- If you want to run library's tests, don't forget to download the lib's dependencies. Project's dependencies are listed in Tangerine/conf/tinypm.json)
$> make libupdate $> make tests
First, create a Engine object and then you'll be able to render html files like this
use Tangerine\Engine;
$engine = new Engine('path/to/cache/folder', 'path/to/views/folder');
$engine->render('index.html');
A common usage of yield section is this one. In your layout file declare a yield section.
<!-- baselayout.html in views folder -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
{% yield content %}
</body>
</html>
Then in your index view, extends this layout and declare a new block.
<!-- index.html in views folder -->
{% extends layout.html %}
{% block content %}
<div class="demo_content">
<p>Welcome to Tangerine engine, nice to meet you !</p>
</div>
{% endblock %}
And your result will be this after rendering with the Tangerine engine
<!-- This is what you'll get, and cached if enabled -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<div class="demo_content">
<p>Welcome to Tangerine engine, nice to meet you !</p>
</div>
</body>
</html>
Basicly, a yield section needs to be filled via a block section. Pretty simple right ?
For convention, when you want to extend a layout use the keywork extends.
But if you want to include another view part, use the keywork include.
To easily write code faster you'll find snippets support for vscode in support folder of the project.
I'm currently learning how to create a langage support so you can have a better highlighting support for the engine.
Lilian Damiens - @damiens_lilian - [email protected]
- Thanks to David Adams and his article called Lightweight Template Engine with PHP released in September 2020 on codeshack.io. It was really helpful to me especially the regex part, I really learned a lot..