Created
August 27, 2012 17:36
-
-
Save krimdomu/3490661 to your computer and use it in GitHub Desktop.
Using Rex Templates
This file contains 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
####################### | |
# Rexfile | |
####################### | |
task "test", sub { | |
my @arr = ("one", "two", "three"); | |
my %hash = ( | |
name => "foo", | |
age => 99, | |
); | |
my @nested = ( | |
{ | |
name => "bar", | |
age => 100, | |
}, | |
{ | |
name => "baz", | |
age => 101, | |
}, | |
); | |
file "test.txt", | |
content => template( "test.txt.tpl", | |
name_of_array_inside_template => \@arr, | |
name_of_hash_inside_template => \%hash, | |
name_of_nested => \@nested, | |
); | |
}; | |
####################### | |
# Template | |
# File: test.txt.tpl | |
####################### | |
This is a simple template: | |
With a list: | |
<% for my $entry ( @{ $::name_of_array_inside_template } ) { %> | |
- <%= $entry %> | |
<% } %> | |
And with some data: | |
<% for my $key ( keys %{ $::name_of_hash_inside_template } ) { %> | |
<%= $key %> -> <%= $::name_of_hash_inside_template->{$key} %> | |
<% } %> | |
And with some nested data: | |
<% for my $entry ( @{ $::name_of_nested } ) { %> | |
Entry: | |
<% for my $key ( keys %{ $entry } ) { %> | |
- <%= $key %> => <%= $entry->{$key} %> | |
<% } %> | |
<% } %> | |
# -------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment