updated example

This commit is contained in:
Tobe O 2017-10-02 13:29:02 -04:00
parent a5204161b9
commit 9d0c89f376
11 changed files with 47 additions and 25 deletions

View file

@ -1,3 +1,4 @@
import 'dart:convert';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_jael/angel_jael.dart';
import 'package:file/local.dart';
@ -16,9 +17,13 @@ main() async {
app.post('/', (RequestContext req, res) async {
var body = await req.lazyBody();
print('Body: $body');
var msg = body['message'] ?? '<unknown>';
return await res
.render('index', {'title': 'Form Submission', 'message': msg});
return await res.render('index', {
'title': 'Form Submission',
'message': msg,
'json_message': JSON.encode(msg),
});
});
app.use(() => throw new AngelHttpException.notFound());

View file

@ -1,6 +1,9 @@
<extend src="layout.jl">
<block name="content">
<i if=message != null>
<script>
window.alert({{- json_message }});
</script>
You said: {{ message }}
</i>
<form action="/" method="post">

View file

@ -12,5 +12,8 @@
<block name="content">
<i>Content goes here.</i>
</block>
<script>
console.info('JAEL :)');
</script>
</body>
</html>

View file

@ -11,7 +11,7 @@ In your `pubspec.yaml`:
```yaml
dependencies:
jael: ^1.0.0-alpha
jael: ^1.0.0-beta
```
# API

View file

@ -15,15 +15,6 @@ class TextNode extends ElementChild {
FileSpan get span => text.span;
}
class ScriptTag extends ElementChild {
final Token script_tag;
ScriptTag(this.script_tag);
@override
FileSpan get span => script_tag.span;
}
abstract class Element extends ElementChild {
static const List<String> selfClosing = const [
'include',

View file

@ -23,7 +23,6 @@ enum TokenType {
slash,
equals,
id,
script_tag,
text,
// Keywords

View file

@ -224,8 +224,6 @@ class Renderer {
buffer.write(child.span.text.trimRight());
else
buffer.write(child.span.text);
} else if (child is ScriptTag) {
buffer.writeln(child.script_tag.span.text);
} else if (child is Interpolation) {
var value = child.expression.compute(scope);

View file

@ -140,7 +140,6 @@ class Parser {
parseHtmlComment() ??
parseInterpolation() ??
parseText() ??
parseScriptTag() ??
parseElement();
HtmlComment parseHtmlComment() =>
@ -148,9 +147,6 @@ class Parser {
Text parseText() => next(TokenType.text) ? new Text(_current) : null;
ScriptTag parseScriptTag() =>
next(TokenType.script_tag) ? new ScriptTag(_current) : null;
Interpolation parseInterpolation() {
if (!next(TokenType.doubleCurlyL)) return null;
var doubleCurlyL = _current;

View file

@ -31,7 +31,6 @@ final Map<Pattern, TokenType> _htmlPatterns = {
'!=': TokenType.nequ,
_string1: TokenType.string,
_string2: TokenType.string,
new RegExp(r'<script[^>]*>[^$]*</script>'): TokenType.script_tag,
new RegExp(r'([A-Za-z][A-Za-z0-9]*-)*([A-Za-z][A-Za-z0-9]*)'): TokenType.id,
};
@ -124,7 +123,8 @@ class _Scanner implements Scanner {
var lastToken = _scanFrom(_htmlPatterns, textStart);
if (lastToken?.type == TokenType.equals || lastToken?.type == TokenType.nequ) {
if (lastToken?.type == TokenType.equals ||
lastToken?.type == TokenType.nequ) {
textStart = null;
scanExpressionTokens();
return;
@ -138,10 +138,11 @@ class _Scanner implements Scanner {
// Fold in the ID into a text node...
tokens.removeLast();
textStart = state;
} else if (lastToken?.type == TokenType.id &&
} else if ((lastToken?.type == TokenType.id ||
lastToken?.type == TokenType.string) &&
tokens.length >= 2 &&
tokens[tokens.length - 2].type == TokenType.text) {
// Append the ID into the old text node
// Append the ID/string into the old text node
tokens.removeLast();
tokens.removeLast();

View file

@ -1,5 +1,5 @@
name: jael
version: 1.0.0-alpha+5
version: 1.0.0-beta
description: A simple server-side HTML templating engine for Dart.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/jael/tree/master/jael

View file

@ -34,7 +34,9 @@ main() {
});
test('mixed', () {
var tokens = scan('<ul number=1 + 2>three{{four > five.six}}</ul>', sourceUrl: 'test.jl').tokens;
var tokens = scan('<ul number=1 + 2>three{{four > five.six}}</ul>',
sourceUrl: 'test.jl')
.tokens;
tokens.forEach(print);
expect(tokens, hasLength(20));
@ -59,4 +61,28 @@ main() {
expect(tokens[18], isToken(TokenType.id, 'ul'));
expect(tokens[19], isToken(TokenType.gt));
});
test('script tag interpolation', () {
var tokens = scan(
'''
<script>
window.alert('a string');
</script>
''',
sourceUrl: 'test.jl',
)
.tokens;
tokens.forEach(print);
expect(tokens, hasLength(8));
expect(tokens[0], isToken(TokenType.lt));
expect(tokens[1], isToken(TokenType.id, 'script'));
expect(tokens[2], isToken(TokenType.gt));
expect(
tokens[3], isToken(TokenType.text, "\n window.alert('a string');\n"));
expect(tokens[4], isToken(TokenType.lt));
expect(tokens[5], isToken(TokenType.slash));
expect(tokens[6], isToken(TokenType.id, 'script'));
expect(tokens[7], isToken(TokenType.gt));
});
}