dox_view 3.2.0
dox_view: ^3.2.0 copied to clipboard
A template engine for the Dox framework, like Edge.js: layouts with components and slots, @if, @each and escaped output in .dox.html files.
dox_view #
A template engine for the Dox framework, like Edge.js: layouts with components and slots, @if, @each and escaped output in .dox.html files.
Views are HTML templates. Dox fills them with your data and sends them as the response.
The dox_view package has a template engine like Edge.js. Templates are .dox.html files in resources/views.
Install #
dart pub add dox_view
Add the service in lib/, so every isolate sets up the engine:
lib/services/view_service.dart:
import 'package:dox_view/dox_view.dart';
class AppViewProvider extends ViewProvider {
AppViewProvider() : super(const ViewConfig());
}
Render a view #
view() renders a template and sends it as HTML. Leave out the .dox.html extension.
DoxResponse show(DoxRequest req) {
return view(req, 'users/show', <String, dynamic>{
'user': user,
'posts': posts,
});
}
This renders resources/views/users/show.dox.html:
<h1>{{ user.name }}</h1>
View.render() gives the HTML as a string, for example for an email:
String html = View.render('emails/welcome', <String, dynamic>{'name': 'Ann'});
Output #
{{ }} prints a value. It escapes HTML, so a user can not add a <script> tag to your page.
<p>Hello {{ user.name }}</p>
{{{ }}} prints a value with no escape. Use it only for HTML you trust.
<article>{{{ post.html }}}</article>
null prints nothing. true prints true, false prints false, and 10.0 prints 10.0. A variable that does not exist prints nothing. The bladeOutput and strictVariables options change this. See Strict options.
A comment is not in the output:
{{-- only you see this --}}
To print {{ or a tag as text, put @ before it: @{{ name }} prints {{ name }}, and @@if prints @if.
Expressions #
Inside {{ }} and tags, you can use:
| What | Example |
|---|---|
| Values | 'text', "text", 42, 1.5, true, false, null |
| Lists and maps | [1, 2], { name: 'Ann', 'full name': 'Ann Lee' } |
| Fields | user.name, user?.name, items[0], map['key'] |
| Math | +, -, *, /, %, ~/ |
| Compare | ==, !=, <, <=, >, >= (=== and !== work too) |
| Logic | &&, ||, !, a ?? b, a ? b : c |
| Functions | route('users.show', { id: 5 }), name.toUpperCase() |
+ joins text: {{ 'Hi ' + name }}.
Fields work on maps, and on objects with a toJson() method, like Dox models.
Lists, maps and text have length, isEmpty and isNotEmpty. Lists also have first, last, contains(), join() and indexOf().
Text has toUpperCase(), toLowerCase(), trim(), contains(), startsWith(), endsWith(), split(), replaceAll(), substring() and indexOf().
Numbers have toStringAsFixed(), round(), floor(), ceil() and abs().
In @if and ?:, these values are false: null, false, 0, '', an empty list and an empty map. With the stringZeroIsFalse option, the text '0' is false too. See Strict options.
Conditions #
@if(user.isAdmin)
<a href="/admin">Admin</a>
@elseif(user.isEditor)
<a href="/posts">Posts</a>
@else
<p>Welcome</p>
@end
@unless(x) is @if(!x).
A tag alone on its line leaves no empty line in the output.
A tag can not come right after a letter or a digit, so me@mail.com stays text. Put a space before the tag, or use {{ on ? 'active' : '' }}.
Loops #
<ul>
@each(user in users)
<li>{{ user.name }}</li>
@else
<li>No users yet</li>
@end
</ul>
The @else part shows when the list is empty.
Get the index too, or the key of a map:
@each((user, index) in users)
{{ index + 1 }}. {{ user.name }}
@end
@each((value, key) in settings)
{{ key }}: {{ value }}
@end
$loop tells you where the loop is:
@each(tag in tags)
{{ tag }}{{ $loop.last ? '' : ', ' }}
@end
| Field | What it is |
|---|---|
$loop.index |
the round, from 0 |
$loop.iteration |
the round, from 1 |
$loop.first |
true in the first round |
$loop.last |
true in the last round |
$loop.count |
the number of items. $loop.length is the same. |
$loop.remaining |
the rounds left after this one |
$loop.even |
true when iteration is even: the 2nd, 4th, ... round |
$loop.odd |
true when iteration is odd: the 1st, 3rd, ... round |
$loop.depth |
1 for a loop, 2 for a loop inside a loop |
$loop.parent |
the $loop of the loop around this one. null when there is none. |
@each(row in rows)
@each(cell in row)
<td class="{{ $loop.parent.odd ? 'light' : 'dark' }}">{{ cell }}</td>
@end
@end
A loop in an @include counts the loop around the @include as its parent. A loop in a component starts at depth 1.
Variables #
@let makes a variable. @assign changes it.
@let(total = 0)
@each(item in cart)
@assign(total = total + item.price)
@end
Total: {{ total }}
Partials #
@include puts another template in this one. It sees the same variables.
@include('partials/nav')
A map as the second argument gives more variables to the template:
@include('partials/card', { title: 'Profile', user: post.author })
- The template sees the variables of the page and the map. On the same name, the map wins.
- The map is only for this include. The page does not see it after the include.
- A second argument that is not a map throws a
ViewException.
@includeIf and @includeWhen include the template only when the condition is true. The two tags do the same. The condition is the first argument, the template name is the second, and the map is the third:
@includeIf(user, 'partials/account')
@includeWhen(user.isAdmin, 'partials/admin', { level: 2 })
More tags #
Close a block tag with @end. @else works in all of them, but not in @once and @verbatim.
Put @auth, @guest, @production, @once and @verbatim alone on their line. In the middle of text, like @once upon a time, they stay text.
With a space before the (, in the middle of a line, a tag is used only when its args are a valid expression. So text like @class (see docs) stays text. With a space, on any line, a tag with a body, like @isset, @empty, @env, @section and @push, also needs its own @end after it. The @end of a block around it does not count. So @empty (none) stays text, also alone on its line. With no space, like @empty(items), a tag is always a tag. These rules also hold:
@extends,@section,@yield,@stack,@push,@prepend,@methodand@error. With a space before the(, these eight are tags only when the first argument is text in quotes, like@yield ('title'). This holds on any line. So text likegit @push (force)oran @error (see log)stays text.@jsonand@includeWhen. They are tags only with the(right after the name, like@json(user). With a space, likethe @json (format), they stay text.
| Tag | What it does |
|---|---|
@isset(user.phone) ... @end |
shows when the value is not null. A field of null is not an error here. |
@empty(items) ... @end |
shows when the value is false: null, false, 0, '', an empty list or map. With stringZeroIsFalse, '0' too. |
@auth ... @end |
shows when the user is logged in (req.auth) |
@guest ... @end |
shows when the user is not logged in |
@env('local') ... @end |
shows when APP_ENV is this value. A list works too: @env(['local', 'staging']). Case does not matter. |
@production ... @end |
shows when APP_ENV is production |
@once ... @end |
shows only the first time in one render, for example a <script> in a component |
@verbatim ... @end |
prints the text as it is: no {{ }} and no tags run inside it |
@extends('layouts/app') |
the layout of the page. See Layouts with sections. |
@section('name') ... @end |
fills a section. Short form: @section('title', 'Home'). |
@yield('name') |
prints a section. @yield('title', 'My app') has a default. |
@stack('scripts') |
prints every push of the stack. See Stacks. |
@push('scripts') ... @end |
adds to the end of a stack |
@prepend('scripts') ... @end |
adds to the start of a stack |
@csrf |
the hidden _token field. See Forms. |
@method('PUT') |
the hidden _method field |
@error('email') ... @end |
shows when the field has a validation error. See Forms. |
@json(value) |
the value as JSON. See JSON for JavaScript. |
@includeWhen(condition, 'name') |
includes a template when the condition is true. See Partials. |
@datetime(...) |
a tag of your app. See Your own tags. |
@auth
<a href="/logout">Log out</a>
@else
<a href="/login">Log in</a>
@end
@auth and @guest use the req.auth that view() gets. In View.render there is no request, so @guest shows. But a $auth key in the data of View.render makes @auth true there. So do not pass data from the user as $auth.
@verbatim ends at the first @end, so the text inside can not have @end. Use it for code of a JavaScript framework that also uses {{ }}:
@verbatim
<div id="app">{{ message }}</div>
@end
These tags print an HTML attribute:
<a @class(['link', { active: isActive, muted: !isActive }])>Home</a>
<input type="checkbox" name="news" @checked(user.news)>
<option value="en" @selected(lang == 'en')>English</option>
<button @disabled(saving)>Save</button>
@classtakes text, a map of class name to a condition, or a list of these. It printsclass="..."with the names whose condition is true. The names are escaped.@checked,@selectedand@disabledprint the attribute name when the value is true.
A tag can not come right after a letter or a digit. Write on @else off, not on@else off.
When resources/views/components/ has a template with the same name, like class.dox.html, the component wins.
Layouts and components #
A component is a template with its own props and slots. Use one for the page layout.
resources/views/layouts/main.dox.html:
<!DOCTYPE html>
<html>
<head>
<title>{{ title ?? 'My app' }}</title>
</head>
<body>
<header>{{ $slots.header }}</header>
<main>{{ $slots.main }}</main>
</body>
</html>
resources/views/home.dox.html:
@component('layouts/main', { title: 'Home' })
@slot('header')
<h1>Welcome</h1>
@end
<p>Hello {{ user.name }}</p>
@end
- The second argument is the props. In the component they are variables (
title), and also$props.title. @slot('name') ... @endfills a named slot. Everything else goes into$slots.main.- A slot is HTML, so
{{ }}does not escape it again. $slotis the same as$slots.main. Both are always there. They are empty when the component has no body.- A component only sees its props,
$props,$slots,$slot,$attributes, the globals and the request helpers ofview()(see Forms). It does not see the variables of the page.
@!component(...) is a component with no body and no @end.
Components as tags #
A template in resources/views/components/ is also a tag with its name. A . in the tag is a folder.
resources/views/components/button.dox.html:
<button class="btn btn-{{ type ?? 'primary' }}">{{ $slots.main }}</button>
@button({ type: 'danger' })
Delete
@end
@!form.input({ name: 'email' }) {{-- components/form/input.dox.html --}}
Attributes #
$attributes has the props as HTML attributes. {{ $attributes }} prints them, like id="save" title="Save". The values are escaped.
$attributes.merge({ ... }) adds default attributes. A prop wins over a default, but class is joined:
<button {{ $attributes.merge({ class: 'btn', type: 'button' }) }}>{{ $slot }}</button>
@button({ class: 'btn-big', type: 'submit' })
Save
@end
This prints <button class="btn btn-big" type="submit">.
Only text, numbers and true are attributes. A number prints like {{ }}, so with bladeOutput 1.0 is 1. true prints the name alone, like hidden. null, false, lists, maps and objects are left out. You can still read any prop, like $attributes.type.
$attributes.except([...]) leaves out props that are only for the logic of the component, like a variant. It can go before merge():
<button {{ $attributes.except(['variant']).merge({ class: 'btn-' + variant }) }}>{{ $slot }}</button>
$attributes keeps any attribute name, like onclick. So do not pass a map from the user as the props of a component.
Layouts with sections #
A page can also name its layout with @extends. The page fills sections. The layout prints them with @yield.
resources/views/layouts/app.dox.html:
<!DOCTYPE html>
<html>
<head>
<title>@yield('title', 'My app')</title>
</head>
<body>
<main>@yield('content')</main>
</body>
</html>
resources/views/home.dox.html:
@extends('layouts/app')
@section('title', 'Home')
@section('content')
<p>Hello {{ user.name }}</p>
@end
@extendsmust be the first thing in the template. A template can have only one.@section('name') ... @endfills a section. The short form@section('title', 'Home')takes a value. The value is escaped, like{{ }}.@yield('name')prints a section. With a second argument, like@yield('title', 'My app'), it prints that value when the page has no such section. The value is escaped too.- In a page with
@extends, only the sections reach the layout. Text outside a section is not printed.@letand@pushoutside a section still work. - A section sees the variables of the page. The layout sees them too.
- A layout can extend another layout. When a page and its layout fill the same section, the page wins.
- The names of sections and stacks must be plain text in quotes, like
'title'. - The first
@sectionwith a name wins. So a@sectioninside@eachkeeps only the first round. - A
@yield('s')that comes before the@section('s')in the same file prints nothing. - An
@includeor a component with its own@extendsprints its layout in place. It shares the section names with the page. - A tag right after a letter or a digit, like
L@yield('s'), stays text.
Use @extends for the page layout. Use a component when you need props or more than one copy on a page.
Stacks #
A stack collects HTML from many templates and prints it in one place. Use it for scripts and styles.
<head>
@stack('styles')
</head>
<body>
@yield('content')
@stack('scripts')
</body>
@push('scripts')
<script src="/js/chart.js"></script>
@end
@push('name') ... @endadds to the end of the stack.@prepend('name') ... @endadds to the start.- A push works from the page, its layout, an
@includeand a component. @stackcan come before the@pushin the page. It still prints every push.- The pushes are printed in the order they run. A page with
@extendsruns before its layout. - Put
@onceinside@pushin a component, so the script is added one time only.
@extends, @section, @yield, @stack, @push, @prepend, @method, @error, @json and @includeWhen need ( ). With no ( ), like @section in a sentence, they stay text. For a space before the (, see the rule in More tags.
Forms #
With SessionMiddleware, view() adds these to every template:
| Name | What it is |
|---|---|
csrfField() |
the hidden _token field for CsrfMiddleware. @csrf prints the same. |
csrfToken() |
the CSRF token |
errors |
the flashed validation errors, like errors.email |
old('email') |
the input of the request before |
session('key') |
a value from the session |
errors is always there. With no session, and in View.render, it is an empty map, so {{ errors.email }} prints nothing.
If your app has a global named errors, the global shows when the request has no validation errors. When there are validation errors, they win over the global.
<form method="POST" action="/register">
{{ csrfField() }}
<input name="email" value="{{ old('email') }}">
@error('email')
<p class="error">{{ message }}</p>
@end
<button>Register</button>
</form>
@error('field') ... @end shows its body when errors has a message for the field. Inside the body, message is that message.
- When the value in
errorsis a list,messageis its first item. - With no error for the field, the body is not printed. An
@elsepart is printed instead, when there is one. - With no session, and in
View.render, there are no errors, so the body is not printed. It does not throw. - A name with dots, like
@error('user.email'), reads the key'user.email'oferrorsfirst. That is how the validator names a nested field. When there is no such key, it readsemailinsideuser. - The name can be a variable, like
@error(name)in an input component. messageis only there inside the body. After@end, amessagevariable of your page has its own value again.
@csrf prints the same hidden field as csrfField(). Put it alone on its line. With no session, @csrf throws a ViewException.
A browser form can only send GET and POST. For a PUT, PATCH or DELETE route, send a POST and add @method. It prints the hidden _method field that the MethodOverride middleware reads:
<form method="POST" action="/posts/{{ post.id }}">
@csrf
@method('PUT')
<input name="title" value="{{ post.title }}">
<button>Save</button>
</form>
This prints <input type="hidden" name="_token" value="..."> and <input type="hidden" name="_method" value="PUT">.
Every template from view() also has request.path, request.method and request.query.
Components and layouts see all of these too, so a layout can use request.path and a form component can use csrfField().
Globals #
Globals are variables and functions that every template can use. route() is there by default.
const ViewConfig(globals: <String, Object?>{'appName': 'My app'});
View.global('year', DateTime.now().year);
View.global('money', (num cents) => (cents / 100).toStringAsFixed(2));
You can add globals before or after View.configure(). The engine keeps them. When a name is in both, the value in ViewConfig.globals wins.
<footer>{{ appName }} {{ year }}</footer>
<a href="{{ route('users.show', { id: user.id }) }}">Profile</a>
<p>{{ money(order.total) }}</p>
A value of type SafeString is printed with no escape:
View.global('logo', const SafeString('<img src="/logo.svg" alt="Logo">'));
JSON for JavaScript #
@json(value) prints a value as JSON. Use it to give data to JavaScript:
<script>
const user = @json(user);
</script>
<div data-tags='@json(tags)'></div>
- Maps, lists, text, numbers,
true,falseandnullwork. An object with atoJson()method works too, like a Dox model. <,>,&,'and the line separators U+2028 and U+2029 are written as\uXXXX. So a value can not close the<script>block or the attribute.- The output is safe in two places: inside a
<script>block, and inside an attribute in single quotes. Do not put it in an attribute in double quotes: JSON has"in it, and that ends the attribute. - A value that can not be JSON, like a
DateTimeor an object with notoJson(), throws aViewException.
json(value) is the same as a function. {{ json(value) }} prints the same text, and does not escape it again. Use it where you need an expression:
@!chart({ points: json(points) })
In Dart, SafeString.json(value) gives the same text.
Your own tags #
A directive is a tag that your app adds. Write a class with a render method. It gets the values between ( and ), and gives the text to print.
class DateTimeDirective implements ViewDirective {
@override
Object? render(List<Object?> args, Scope scope) {
Object? value = args[0];
DateTime time = value is DateTime ? value : DateTime.parse('$value');
return '${time.day}/${time.month}/${time.year}';
}
}
View.directive('datetime', DateTimeDirective());
<time>@datetime(post.createdAt)</time>
- The text is escaped, like
{{ }}. Give aSafeStringto print HTML as it is.nullprints nothing. scope.lookup('name')reads a variable of the template.- A directive has no body and no
@end. - The tag needs
( ). With no( ), like@datetimein a sentence, it stays text. - The name can have letters, digits and
_. The name of a built-in tag, likeif,sectionorjson, throws aViewException. - When
resources/views/components/has a template with the same name, the component wins. - You can also give them in the config:
ViewConfig(directives: <String, ViewDirective>{'datetime': DateTimeDirective()}).
You can add directives before or after View.configure(). The engine keeps them.
Config #
| Option | Default | What it does |
|---|---|---|
path |
resources/views |
the folder of the templates |
extension |
.dox.html |
the file extension |
cache |
on, but off when APP_ENV is development or local |
keep parsed templates in memory |
globals |
{} |
variables for every template |
directives |
{} |
your own tags, by name |
strictVariables |
false |
a variable that does not exist throws a ViewException. See Strict options. |
stringZeroIsFalse |
false |
the text '0' is false in a condition |
bladeOutput |
false |
print true as 1, false as nothing and 10.0 as 10 |
bladeWarnings |
true |
warn about Blade names that are not Dox tags. See Coming from Blade. |
With the cache off, a changed template shows on the next request, with no restart.
Strict options #
These three options are off by default. Turn each one on in ViewConfig:
const ViewConfig(
strictVariables: true,
stringZeroIsFalse: true,
bladeOutput: true,
);
strictVariables: a variable that does not exist throws a ViewException. The message has the name of the variable, the template and the line. So a typo like {{ titel }} shows as an error, not as an empty spot on the page. When it is off, a variable that does not exist prints nothing.
A variable that is there with the value null is fine. These ways to read a variable that may not exist never throw:
| Code | What it gives when user does not exist |
|---|---|
@isset(user) ... @end |
the body is not printed |
@empty(user) ... @end |
the body is printed |
{{ user ?? 'Guest' }} |
Guest |
{{ user?.name }} |
nothing |
The rule covers the variable on the left of ?? and ?.. In {{ a ?? b ?? 'x' }}, both a and b can be missing. {{ user.name ?? 'Guest' }} still throws when user does not exist: write {{ user?.name ?? 'Guest' }}.
stringZeroIsFalse: the text '0' is false in @if, @unless, @empty, ?:, &&, ||, !, @class, @checked, @selected, @disabled, @includeIf and @includeWhen. Other text, like '0.0' or '00', is still true. When it is off, '0' is true, like any text that is not empty.
bladeOutput: {{ }} and {{{ }}} print true as 1, false as nothing, and a number with no fraction, like 10.0, as 10. The values of @yield, @section and @method are printed the same way. Joining text with + does not change: {{ 'a' + true }} is still atrue. When it is off, true prints true, false prints false, and 10.0 prints 10.0.
Coming from Blade #
Some Blade names are not Dox tags. Dox prints them as plain text. To help you find them, Dox writes a warning to stderr when it reads a template that has one:
dox_view: warning: posts/index:12 "@foreach" is not a Dox tag. Use "@each(item in items) ... @end".
| Blade | Dox |
|---|---|
{!! html !!} |
{{{ html }}} |
@foreach, @forelse, @for |
@each(item in items) ... @else ... @end |
@endif, @endforeach, @endsection and the other @end... names, @stop |
@end |
@switch |
@if ... @elseif ... @else ... @end |
@php, @endphp |
@let(name = value) |
@show |
@end, then @yield('name') |
@parent |
not there. Use @stack and @push to add content. |
- The output of the template does not change. The warning is only a hint.
- Each warning is written one time.
@end...names warn anywhere in a line. The other names warn only with the(right after the name, like@foreach(...), or alone on their line. So a sentence likethe @foreach loopdoes not warn.- Text in a
{{-- --}}comment, in@verbatim, and a name with@@before it, do not warn. A name in an HTML comment, like<!-- @endif -->, does warn. ViewConfig(bladeWarnings: false)turns the warnings off.
Errors #
A syntax error, or an error in an expression, throws a ViewException. The message has the template name and the line:
ViewException: expected ")" but found the end (in "users/show", line 12)
Prettier #
Prettier formats .html files, and it does not know the @ tags. Add this line to .prettierignore:
*.dox.html
Credits #
The syntax and the ideas of this package come from Edge.js, the template engine of AdonisJS, made by Harminder Virk and the AdonisJS team. Thank you!
dox_view is a new engine written in Dart. It does not use Edge.js code.