https://github.com/maplephp/swiftrender
Advanced PHP template engine built for MaplePHP
https://github.com/maplephp/swiftrender
Last synced: 8 months ago
JSON representation
Advanced PHP template engine built for MaplePHP
- Host: GitHub
- URL: https://github.com/maplephp/swiftrender
- Owner: MaplePHP
- License: apache-2.0
- Created: 2023-03-29T11:37:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-25T19:08:16.000Z (about 1 year ago)
- Last Synced: 2025-09-27T20:14:27.144Z (9 months ago)
- Language: PHP
- Homepage:
- Size: 99.6 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftRender
PHP SwiftRender is a pure PHP advanced template library. It offers several advantages for developers who need to create and render templates in PHP applications. Firstly, it can improve performance, as there is no need for an additional language or engine to process the template. This can result in faster rendering times and reduced overhead. Additionally, a pure PHP template library is highly portable, as it can be used in almost any PHP application, regardless of the underlying platform or framework.
PHP SwiftRender offers greater flexibility and control over the rendering process. Developers that knows PHP can structure and design their templates in ways that better suit their specific needs, resulting in more efficient and effective templates.
PHP is a widely-used language, most developers are already familiar with its syntax and conventions. This makes it easier to learn and use a pure PHP template library than an entirely new templating language or engine.
## Usage
### Initialisation
One time setup to use through the application.
```php
use MaplePHP\Output\SwiftRender;
use MaplePHP\DTO\Format;
$swift = new SwiftRender();
$swift->setIndexDir(dirname(__FILE__)."/resources/") // Set index directory
->setViewDir(dirname(__FILE__)."/resources/views/") // Set view directory
->setPartialDir(dirname(__FILE__)."/resources/partials/"); // Set partials directory
// Prepare/bind "/resources/index.php"
$swift->setIndex("index");
// Prepare/bind "/resources/views/main.php"
$swift->setView("main");
// Prepare/bind "/resources/partials/article.php"
$swift->setPartial("article", [
"date" => "2023-02-30 15:33:22",
"name" => "This is an article",
"content" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
"feed" => [
[
"headline" => "test 1",
"description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto."
],
[
"headline" => "test 2",
"description" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, architecto."
]
]
]);
// Keep in mind that the data usually comes from the database and that it should/might be called from you controller.
// E.g. $swift->setPartial("article", $mysqli->fetch_objects());
```
### Templating
#### Index
The file **/resources/index.php** has already been bounded under the initialisation section above **$swift->setIndex("index")**. The file looks like this:
```html
execute(); ?>
partial("navigation")->get(); ?>
view()->get(); ?>
```
#### View
The file **/resources/views/main.php** has already been bounded under the initialisation section above **$swift->setView("main")**. The file looks like this:
```html
partial("article")->get(); ?>
```
#### Partial
The file **/resources/partials/article.php** has already been bounded under the initialisation section above **$swift->setPartial("article", ...)**. The file looks like this:
```html
clockFormat("Y/m/d"); ?>
stExcerpt(20); ?>
count() > 0): ?>
-
headline->strUcFirst(); ?>
description; ?>
fetch() as $row): ?>
```
#### Partial functionality
The partials all arguments will automatically be converted to an object with a lot of extended functionality. Here is some:
```php
echo $date; // 2023-02-30 15:33:22
echo $date->clockFormat("Y/m/d"); // 2023/02/30
// Will strip all html tags, replace regular line breaks with "
" and uppercase the first letter
echo $content->strStripTags()->strNl2br()->strUcfirst();
// Loop through an array
if($feed->count() > 0) foreach($feed->fetch() as $row) {
echo $row->headline->strUcFirst() . "
";
}
```
#### Run the template engine
You can run the template engine later in an empty file, emitter or router dispatcher. It all depends on your setup.
```php
echo $swift->index()->get();
```
#### Dynamic views
You can also create a dynamic view that will overwrite the current view if called. This is great for e.g. showing a 404 page.
In this example the current view which is **/resources/views/main** will be replaced with the view **/resources/views/httpStatus.php** when response status code is (403, 404 e.g.).
```php
// MaplePHP framework (PSR response). Just using this in this example to handle status response codes
use MaplePHP\Http\Response;
$swift->bindToBody(
"httpStatus",
Format\Arr::value(Response::PHRASE)->unset(200, 201, 202)->arrayKeys()->get()
// This method will load all HTTP Request status codes (like 403, 404 e.g.) except for (200, 201, 202)
);
$swift->findBind($response->getStatusCode());
```
#### Easy DOM manipulation
Advance DOM creation and works great with stuff like the Metadata because you can later in change values and attributes in the controller.
```php
// Advance DOM creation and works great with stuff like the Metadata
$dom = MaplePHP\Output\Dom\Document::dom("head");
$dom->bindTag("title", "title")->setValue("Meta title");
$dom->bindTag("meta", "description")->attr("name", "Description")->attr("content", "Lorem ipsum dolor sit amet.");
// Then later in controller you can change the meta title and description
$head = MaplePHP\Output\Dom\Document::dom("head");
$head->getElement("title")->setValue("New meta title");
$head->getElement("description")->attr("content", "New meta description...");
```