Skip to content

marko/view-latte

Latte templating driver for the Marko Framework. Implements ViewInterface from marko/view using the Latte engine, with module-namespaced template resolution, automatic caching, and strict types.

Terminal window
composer require marko/view-latte

This automatically installs marko/view.

Configure via the view config key:

config/view.php
return [
'view' => [
'cache_directory' => '/path/to/cache',
'extension' => '.latte',
'auto_refresh' => true, // Set false in production
'strict_types' => true,
],
];

Templates are rendered using the module namespace syntax:

use Marko\View\ViewInterface;
$view->render('blog::post/index', ['posts' => $posts]);

The format is module::path/to/template where:

  • module is the module name (e.g., blog, admin)
  • path/to/template is the path within resources/views/

The render() method returns an HTTP Response object. Use renderToString() when you need the raw HTML --- for example, when rendering email bodies:

use Marko\View\ViewInterface;
$html = $view->renderToString('blog::email/comment-verification/html', $data);
$text = $view->renderToString('blog::email/comment-verification/text', $data);

All template includes use the same namespaced syntax:

{include 'blog::post/list/item', post: $post}
{include 'blog::pagination/index', pagination: $posts}

Relative paths (../) are not supported. This ensures:

  • Consistent syntax throughout templates
  • Templates can include from any module
  • No fragile relative path dependencies

Pass variables to included templates as named parameters:

{include 'blog::post/list/item', post: $post, showAuthor: true}

Default values in the included template:

resources/views/post/list/item.latte
{* post/list/item.latte *}
{default $showAuthor = true}
{default $linkAuthor = true}
<li class="post-item">
<h2><a href="/blog/{$post->slug}">{$post->title}</a></h2>
{if $showAuthor}
<span>By {$post->getAuthor()->name}</span>
{/if}
</li>

All templates must live within at least one directory. No top-level template files.

views/
post/
index.latte # Post listing
show.latte # Single post
list/
item.latte # Reusable list item
category/
show.latte
tag/
index.latte
author/
show.latte
search/
index.latte
bar.latte # Search input
comment/
form.latte
thread.latte
pagination/
index.latte

Email templates group HTML and plain text versions together:

views/
email/
comment-verification/
html.latte # HTML version
text.latte # Plain text version
welcome/
html.latte
text.latte

LatteView implements ViewInterface from marko/view.

MethodDescription
render(string $template, array $data = []): ResponseRender a template and return an HTTP response
renderToString(string $template, array $data = []): stringRender a template and return the raw HTML string
OptionTypeDescription
cache_directorystringDirectory for compiled template cache
extensionstringTemplate file extension (default .latte)
auto_refreshboolRecompile templates when source changes --- set false in production
strict_typesboolEnable strict type checking in templates