diff --git a/README.md b/README.md index 44fd246..fcbae70 100644 --- a/README.md +++ b/README.md @@ -207,15 +207,58 @@ Parses Haml string building a tree. ## `render` - my $text = $haml->render('%p foo'); +$haml->render(Haml_string: Str [, %vars: Hash]): Str - my $text = $haml->render('%p var', var => 'hello'); + my $text = $haml->render('%p foo'); #

foo

-Renders Haml string. Returns undef on error. See error attribute. + my $text = $haml->render('%p= $var', var => 'hello'); #

hello

+ + my %foo = ( bar => 'hello', baz => 'world', ); + my $text = $haml->render('%p= "$bar $baz"', %foo); #

hello world

+ + my %foo = ( bar => 'hello', baz => 'world', ); + my $text = $haml->render('%p= "$var->{bar} $var->{baz}"', var => \%foo); #

hello world

+ + my %foo = (var => { bar => 'hello', baz => 'world' }); + my $text = $haml->render('%p= $var->{bar} ." ". $var->{baz}', %foo); #

hello world

+ + my %foo = (var => [ qw/hello world/ ]); + my $page = $haml->render('%p= "$var->[0] $var->[1]"', %foo); #

hello world

+ +Gets Haml string and optional variables. Returns undef on error. See error attribute. ## `render_file` - my $text = $haml->render_file('foo.haml', var => 'hello'); +$haml->render_file(filename: Str [, %vars: Hash]): Str + +The file name can be a file on disk (the default path is the current directory) or virtual path to the file (if you use the module Data::Section::Simple). +The file path specified in the constructor of a template engine in attribute 'path' + + # Current directory is a default. + my $haml = Text::Haml->new; + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + # Set directory 'template' + my $haml = Text::Haml->new(path => 'template'); + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + # Set virtual path (with Data::Section::Simple) + use Data::Section::Simple qw/get_data_section/; + + my $vpath = get_data_section; + + my $haml = Text::Haml->new(path => [$vpath]); + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + __DATA__ + + @@ foo.haml + %p= "$var->{bar} $var->{baz}" + +For more examples with variables see render method A helper method that loads a file and passes it to the render method. Since "%\_\_\_\_vars" is used internally, you cannot use this as parameter name. diff --git a/lib/Text/Haml.pm b/lib/Text/Haml.pm index 85dba5d..f566c28 100644 --- a/lib/Text/Haml.pm +++ b/lib/Text/Haml.pm @@ -1481,15 +1481,58 @@ Parses Haml string building a tree. =head2 C - my $text = $haml->render('%p foo'); +$haml->render(Haml_string: Str [, %vars: Hash]): Str - my $text = $haml->render('%p var', var => 'hello'); + my $text = $haml->render('%p foo'); #

foo

-Renders Haml string. Returns undef on error. See error attribute. + my $text = $haml->render('%p= $var', var => 'hello'); #

hello

+ + my %foo = ( bar => 'hello', baz => 'world', ); + my $text = $haml->render('%p= "$bar $baz"', %foo); #

hello world

+ + my %foo = ( bar => 'hello', baz => 'world', ); + my $text = $haml->render('%p= "$var->{bar} $var->{baz}"', var => \%foo); #

hello world

+ + my %foo = (var => { bar => 'hello', baz => 'world' }); + my $text = $haml->render('%p= $var->{bar} ." ". $var->{baz}', %foo); #

hello world

+ + my %foo = (var => [ qw/hello world/ ]); + my $page = $haml->render('%p= "$var->[0] $var->[1]"', %foo); #

hello world

+ +Gets Haml string and optional variables. Returns undef on error. See error attribute. =head2 C - my $text = $haml->render_file('foo.haml', var => 'hello'); +$haml->render_file(filename: Str [, %vars: Hash]): Str + +The file name can be a file on disk (the default path is the current directory) or virtual path to the file (if you use the module Data::Section::Simple). +The file path specified in the constructor of a template engine in attribute 'path' + + # Current directory is a default. + my $haml = Text::Haml->new; + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + # Set directory 'template' + my $haml = Text::Haml->new(path => 'template'); + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + # Set virtual path (with Data::Section::Simple) + use Data::Section::Simple qw/get_data_section/; + + my $vpath = get_data_section; + + my $haml = Text::Haml->new(path => [$vpath]); + my %foo = ( bar => 'hello', baz => 'world', ); + my $page = $haml->render_file('foo.haml', var => \%foo); #

hello world

+ + __DATA__ + + @@ foo.haml + %p= "$var->{bar} $var->{baz}" + +For more examples with variables see render method A helper method that loads a file and passes it to the render method. Since "%____vars" is used internally, you cannot use this as parameter name.