Skip to content

Instantly share code, notes, and snippets.

@krimdomu
Created August 27, 2012 17:36
Show Gist options
  • Save krimdomu/3490661 to your computer and use it in GitHub Desktop.
Save krimdomu/3490661 to your computer and use it in GitHub Desktop.
Using Rex Templates
#######################
# 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