Skip to content

Instantly share code, notes, and snippets.

@4d47
Created October 5, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4d47/3a6a31fee81dea1f82dd20bf65304285 to your computer and use it in GitHub Desktop.
Save 4d47/3a6a31fee81dea1f82dd20bf65304285 to your computer and use it in GitHub Desktop.
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