A small template engine built for dart.
The main symbol used in the SharkDart is @
. There are several simple rules:
-
@
escape:@@
->@
-
expression
- simple:
@name
, or@{name}
- complex:
@{name.toLowerCase()}
- simple:
-
tag
- param list:
@author(String name: 'Freewind', love: 'programming') { Thanks for PetitParserDart! }
- expression:
@if(a==b) { <div>Hello</div> }
- no-params:
@markdown { **hello** }
- no-body:
@include(a.txt)
- param list:
-
plain-text tag
Use
@!
instead of@
, the content of block will be transformed, e.g.@!dart {{{{{{ // here are some dart code String hello() { print('Hello, world!'); } }}}}}}
You can repeat
{
and}
, to make unique start and end of the block. PS: The same to normal tags@tag {{{ .. }}}
SharkDart is flexible, we can use dart code in template, by:
@!dart {
// you dart code
}
But since dart doesn't support eval
code dynamically,
shark template have to be compiled to dart code,
and invoked as functions in our program.
Say there is a hello.shark
, which will be compiled to hello.dart
,
and we will invoke it like:
// import generated hello.dart from somewhere
import 'hello.dart' as hello;
String result = hello.render({name: 'world'});
@params(String user, List<String> friends)
@extends(./layout, user: 'Shark')
@renderBody()
@if(name=='Shark') { ... }
@elseif(name=='Dart') { ... }
@else { ... }
@for(user: users, separator: ',') { ... }
@render(./another, title: 'Left') { ... }
@!dart { ... }
@!plainText(trim:true) { ... }
You can see the usage in the 'test/templates/tags' directory.
Thanks for the great parser PetitParserDart, and the author @renggli