Feature/smarty example (#71)
Some checks failed
Tests / build (push) Has been cancelled
Release / Release (push) Has been cancelled

* add new example for using smarty templating

* update readme

* Update examples/context-view-smarty/README.md

---------

Co-authored-by: Dominik Prinzensteiner <dominik@prinzensteiner.at>
Co-authored-by: Jamie Barton <jamie@notrab.dev>
This commit is contained in:
Dominik Prinzensteiner 2024-11-19 10:02:06 +01:00 committed by GitHub
parent e4bf36ac07
commit 19819ca8d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,23 @@
# Smarty Example
This example demonstrates how to pass a render closure to context and generate a view using the smarty template engine.
## Running the Example
1. Install dependencies:
```bash
composer install
```
2. Start the server:
```bash
composer start
```
3. Access the demo route:
```bash
curl http://localhost:8000
```

View File

@ -0,0 +1,18 @@
{
"require": {
"notrab/dumbo": "@dev",
"smarty/smarty": "^3.1"
},
"repositories": [
{
"type": "path",
"url": "../../"
}
],
"scripts": {
"start": [
"Composer\\Config::disableProcessTimeout",
"php -S localhost:8000 -t ."
]
}
}

View File

@ -0,0 +1,45 @@
<?php
require __DIR__ . "/vendor/autoload.php";
use Dumbo\Dumbo;
$app = new Dumbo();
$app->use(function ($context, $next) {
$context->render(
function (string $view, array $attributes) {
// Define our cache and views directory
$cacheDirectory = __DIR__ . '/cache/views';
$templateDirectory = __DIR__ . '/views';
$configDirectory = __DIR__ . '/config';
$compileDirectory = __DIR__ . '/cache/compiles';
// Instantiate our Template Engine
$smarty = new Smarty();
$smarty->setTemplateDir($templateDirectory);
$smarty->setCompileDir($compileDirectory);
$smarty->setCacheDir($cacheDirectory);
$smarty->setConfigDir($configDirectory);
// Render our HTML string from our view and attributes.
$smarty->assign($attributes);
$html = $smarty->fetch($view);
return $html;
}
);
return $next($context);
});
$app->get("/", function ($context) {
$view = $context->view('home.tpl', [
'message' => 'Hello Dumbo'
]);
return $view;
});
$app->run();

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Dumbo</title>
</head>
<body>
<h1>
{$message}
</h1>
</body>
</html>