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 @@ + + +
+