Allow @
for passing values to elements
This commit is contained in:
parent
dd205ea693
commit
2339bfa92b
5 changed files with 36 additions and 8 deletions
|
@ -1,3 +1,6 @@
|
|||
# 1.0.6+1
|
||||
* Ensure `<element>` pass attributes.
|
||||
|
||||
# 1.0.6
|
||||
* Add `index-as` to `for-each`.
|
||||
* Support registering + rendering custom elements.
|
||||
|
|
|
@ -353,9 +353,11 @@ class Renderer {
|
|||
var attrs = element.attributes.where((a) => a.name != 'as');
|
||||
|
||||
for (var attribute in attrs) {
|
||||
scope.create(attribute.name,
|
||||
if (attribute.name.startsWith('@')) {
|
||||
scope.create(attribute.name.substring(1),
|
||||
value: attribute.value?.compute(scope), constant: true);
|
||||
}
|
||||
}
|
||||
|
||||
if (renderAs == false) {
|
||||
for (int i = 0; i < template.children.length; i++) {
|
||||
|
@ -369,7 +371,8 @@ class Renderer {
|
|||
var syntheticElement = new RegularElement(
|
||||
template.lt,
|
||||
new SyntheticIdentifier(tagName),
|
||||
[],
|
||||
element.attributes
|
||||
.where((a) => a.name != 'as' && !a.name.startsWith('@')),
|
||||
template.gt,
|
||||
template.children,
|
||||
template.lt2,
|
||||
|
|
|
@ -6,7 +6,7 @@ import '../ast/ast.dart';
|
|||
final RegExp _whitespace = new RegExp(r'[ \n\r\t]+');
|
||||
|
||||
final RegExp _id =
|
||||
new RegExp(r'(([A-Za-z][A-Za-z0-9_]*-)*([A-Za-z][A-Za-z0-9_]*))');
|
||||
new RegExp(r'@?(([A-Za-z][A-Za-z0-9_]*-)*([A-Za-z][A-Za-z0-9_]*))');
|
||||
final RegExp _string1 = new RegExp(
|
||||
r"'((\\(['\\/bfnrt]|(u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])))|([^'\\]))*'");
|
||||
final RegExp _string2 = new RegExp(
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: jael
|
||||
version: 1.0.6
|
||||
version: 1.0.6+1
|
||||
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
|
||||
|
|
|
@ -11,7 +11,7 @@ void main() {
|
|||
<element name="square-root">
|
||||
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
||||
</element>
|
||||
<square-root n=16 />
|
||||
<square-root @n=16 />
|
||||
</div>
|
||||
''';
|
||||
|
||||
|
@ -33,7 +33,7 @@ void main() {
|
|||
<element name="square-root">
|
||||
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
||||
</element>
|
||||
<square-root as="span" n=16 />
|
||||
<square-root as="span" @n=16 />
|
||||
</div>
|
||||
''';
|
||||
|
||||
|
@ -49,13 +49,35 @@ void main() {
|
|||
'''.trim());
|
||||
});
|
||||
|
||||
test('pass attributes', () {
|
||||
var template = '''
|
||||
<div>
|
||||
<element name="square-root">
|
||||
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
||||
</element>
|
||||
<square-root foo="bar" baz="quux" @n=16 />
|
||||
</div>
|
||||
''';
|
||||
|
||||
var html = render(template, {'sqrt': sqrt});
|
||||
print(html);
|
||||
|
||||
expect(html, '''
|
||||
<div>
|
||||
<div foo="bar" baz="quux">
|
||||
The square root of 16 is 4.
|
||||
</div>
|
||||
</div>
|
||||
'''.trim());
|
||||
});
|
||||
|
||||
test('render without tag name', () {
|
||||
var template = '''
|
||||
<div>
|
||||
<element name="square-root">
|
||||
The square root of {{ n }} is {{ sqrt(n).toInt() }}.
|
||||
</element>
|
||||
<square-root as=false n=16 />
|
||||
<square-root as=false @n=16 />
|
||||
</div>
|
||||
''';
|
||||
|
||||
|
|
Loading…
Reference in a new issue