mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-16 13:50:03 +01:00
Feature/smarty example (#71)
* 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:
parent
e4bf36ac07
commit
19819ca8d1
23
examples/context-view-smarty/README.md
Normal file
23
examples/context-view-smarty/README.md
Normal 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
|
||||||
|
```
|
18
examples/context-view-smarty/composer.json
Normal file
18
examples/context-view-smarty/composer.json
Normal 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 ."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
45
examples/context-view-smarty/index.php
Normal file
45
examples/context-view-smarty/index.php
Normal 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();
|
11
examples/context-view-smarty/views/home.tpl
Normal file
11
examples/context-view-smarty/views/home.tpl
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Dumbo</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>
|
||||||
|
{$message}
|
||||||
|
</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user