Templating & Rendering
Rendering & templating in LENKRAD - the basics
About rendering
It's important to understand how LENKRAD handles rendering in general. This page assumes that the default Renderer is used. Please refer to Response and Custom response handling for possibilities to influence rendering.
The Render class
The Renderer class has the following static methods:
setTemplatePath(string $path)
Sets the entry level of template-arguments
getInstance(Renderer $mockInstance = null)
Returns an existing instance if present, creates a new instance if initial or overrides the used instance with a mock instance (testing)
setHtmlSkeleton(string $skeletonPath, string $placement = 'main', array $skeletonVariables = [])
See example below
render(array | DataNormalization $data, $view)
Manually triggers rendering (used automatically on last procedural chain, you likely will never need it)
detachInstance()
Resets internal singleton to null
getTemplatePath()
Returns string with set template path
getHtmlSkeletonPath()
Returns string of set skeleton path
getHtmlComponentPlacement()
Returns string of set component placement
The default rendering class can either be used with individually rendered HTML files or using a surrounding layout where route-specific content is placed in a predefined container. Here, we assume the latter, also referred to as skeleton setup.
Skeleton structure
/src/views/skeleton.html
This file contains the "frame"-html. Maybe header, menus, footer, etc.
Respective component/route HTML
The render class places the route's defined HTML at a specified location (defaults to "main")
The method setHtmlSkeleton is best explained by example.
Example
1. /src/views/skeleton.html (regarding the curly bracket markup, see the Template class)
2. /src/Home/Home.php
?php
namespace App\Home;
use Neoan\Routing\Interfaces\Routable;
class Home implements Routable
{
public function __invoke(): array
{
return ['fromHome' => 'Hello there, Obi Wan Kenobi']
}
}
3. /src/Home/view.html
4. /public/index.php
use Neoan\NeoanApp;
use Neoan\Helper\Setup;
use Neoan\Routing\Route;
use Neoan\Render\Renderer;
use Neoan\Enums\ResponseOutput;
use Neoan\Response\Response;
use App\Home\Home;
$projectPath = dirname(__DIR__);
require_once $projectPath . '/vendor/autoload.php';
$setup = new Setup();
$setup->setPublicPath($projectPath . '/public')
->setLibraryPath($projectPath . '/src')
// set template path relative to composer file path
->setTemplatePath('src/')
// enable the use of a skeleton
->setUseSkeleton(true)
// what is our skeleton HTML?
->setSkeletonHTML('src/views/skeleton.html')
// which variable to use for component placement
->setSkeletonComponentPlacement('main')
// finally, let's set default variables
->setSkeletonVariables([
'year' => '2022',
'title' => 'My App'
]);
$app = new NeoanApp($setup, $publicPath);
// register a test route
Route::get('/', Home::class)->view('Home/view.html');
// run application
$app->run();
We're lumping everything in the index file to show everything at one glance. Feel free to have a look at our starter projects to get suggestions of how to structure your real-life project.
The Template class
LENKRAD ships with neoan3-apps/template as the default templating mechanism. Documentation for the package is provided, so here we only provide a very basic example to get you started.
e.g. in /src/MyRoute/MyRoute.php
...
#[Web('/my-route', '/MyRoute/view.html')]
class MyRoute implements Routable
{
public function __invoke(NeoanApp $app): array
{
$search = new Search();
// Let's spoof a search
Request::setQueries(['query' => 'model']);
return [
'boolean' => true,
'results' => $search($app)['sections'],
'today' => date('m/d/Y')
];
}
}
e.g. in /src/MyRoute/view.html
Result
11/23/2024
/docs/models#class | THE MODEL CLASS |
/docs/models#define | DEFINING A MODEL |
/docs/models#usage | USING A MODEL |
For translations and internationalization, you can use PHP's gettext or a package like php-i18n-translate
Before you move on
Many references on this page assume default settings. Your project might differ in behavior, paths etc.