Created
October 5, 2016 01:59
-
-
Save 4d47/3a6a31fee81dea1f82dd20bf65304285 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
use Template::Anti; | |
multi process($root, %data where { .values.all ~~ Cool }) { | |
# Treat content attribute specially | |
if %data<content>:exists { | |
$root.text(%data<content>); # this should be 'content' | |
%data<content>:delete; | |
} | |
# Apply remaining hash values as node attributes | |
$root.attrib(|%data); | |
} | |
multi process($root, %data where { .values.all ~~ Array }) { | |
for %data.kv -> $selector, @data { | |
$root.find($selector).apply(@data).via: &process | |
} | |
} | |
my $anti = Template::Anti.load(' | |
<ul class="people"> | |
<li><a href="#">Name</a></li> | |
</ul> | |
'); | |
my $rules = { 'ul.people li' => [ | |
{ 'a' => [ { content => 'Vader', href => 'http://example.com/vader' }, ] }, | |
{ 'a' => [ { content => 'Sidious', href => 'http://example.com/sidious' }, ] } | |
] }; | |
process($anti, $rules); | |
put $anti.render; | |
# selector => Array of Hash | |
# means node.find(Str).apply(Array).via: -> $node, %model { ... } | |
# attribute => Str | |
# means node.attrib(| | |
# where content attribute is special | |
# Str => Hash | |
# alias for array of single hash | |
# Str => | |
# | |
# 'ul.people li' => [ | |
# { 'a' => [ { text => 'Vader', href => 'http://example.com/vader' } ] }, | |
# { 'a' => [ { text => 'Sidious', href => 'http://example.com/sidious' } ] } | |
# ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment