From 19819ca8d12a4d54048ad2f9cbb6afcf4b7460fa Mon Sep 17 00:00:00 2001 From: Dominik Prinzensteiner Date: Tue, 19 Nov 2024 10:02:06 +0100 Subject: [PATCH] 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 Co-authored-by: Jamie Barton --- examples/context-view-smarty/README.md | 23 +++++++++++ examples/context-view-smarty/composer.json | 18 +++++++++ examples/context-view-smarty/index.php | 45 +++++++++++++++++++++ examples/context-view-smarty/views/home.tpl | 11 +++++ 4 files changed, 97 insertions(+) create mode 100644 examples/context-view-smarty/README.md create mode 100644 examples/context-view-smarty/composer.json create mode 100644 examples/context-view-smarty/index.php create mode 100644 examples/context-view-smarty/views/home.tpl diff --git a/examples/context-view-smarty/README.md b/examples/context-view-smarty/README.md new file mode 100644 index 0000000..67acb1a --- /dev/null +++ b/examples/context-view-smarty/README.md @@ -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 + ``` diff --git a/examples/context-view-smarty/composer.json b/examples/context-view-smarty/composer.json new file mode 100644 index 0000000..f0777d7 --- /dev/null +++ b/examples/context-view-smarty/composer.json @@ -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 ." + ] + } +} diff --git a/examples/context-view-smarty/index.php b/examples/context-view-smarty/index.php new file mode 100644 index 0000000..da12135 --- /dev/null +++ b/examples/context-view-smarty/index.php @@ -0,0 +1,45 @@ +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(); diff --git a/examples/context-view-smarty/views/home.tpl b/examples/context-view-smarty/views/home.tpl new file mode 100644 index 0000000..a591fa7 --- /dev/null +++ b/examples/context-view-smarty/views/home.tpl @@ -0,0 +1,11 @@ + + + + Dumbo + + +

+ {$message} +

+ +