Skip to content

Commit 7d0112d

Browse files
committed
examples: added Latte example
1 parent 62f096c commit 7d0112d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

examples/latte.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* Nette Forms rendering using Latte.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
10+
if (@!include __DIR__ . '/../vendor/autoload.php') {
11+
die('Install packages using `composer install`');
12+
}
13+
14+
use Nette\Forms\Form;
15+
use Tracy\Debugger;
16+
use Tracy\Dumper;
17+
18+
Debugger::enable();
19+
20+
21+
$form = new Form;
22+
$form->addText('name', 'Your name:')
23+
->setRequired('Enter your name');
24+
25+
$form->addPassword('password', 'Choose password:')
26+
->setRequired('Choose your password')
27+
->addRule($form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
28+
29+
$form->addPassword('password2', 'Reenter password:')
30+
->setRequired('Reenter your password')
31+
->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
32+
33+
$form->addSubmit('submit', 'Send');
34+
35+
if ($form->isSuccess()) {
36+
echo '<h2>Form was submitted and successfully validated</h2>';
37+
Dumper::dump($form->getValues());
38+
exit;
39+
}
40+
41+
$latte = new Latte\Engine;
42+
$latte->onCompile[] = function ($latte) {
43+
Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
44+
};
45+
46+
$latte->render('template.latte', ['form' => $form]);

examples/template.latte

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Nette Forms rendering using Latte</title>
6+
<link rel="stylesheet" media="screen" href="assets/style.css" />
7+
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script>
8+
</head>
9+
10+
<body>
11+
<h1>Nette Forms rendering using Latte</h1>
12+
13+
<form n:name=$form>
14+
<ul class=error n:ifcontent>
15+
<li n:foreach="$form->ownErrors as $error">{$error}</li>
16+
</ul>
17+
18+
<table>
19+
<tr n:foreach="$form->controls as $input"
20+
n:if="!$input->getOption(rendered) && $input->getOption(type) !== hidden"
21+
n:class="$input->required ? required">
22+
23+
<th>{label $input /}</th>
24+
<td>{input $input} <span class=error n:ifcontent>{$input->error}</span></td>
25+
</tr>
26+
</table>
27+
</form>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)