| 1 | package Catalyst::Helper::View::Haml; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | Catalyst::Helper::View::Haml - Helper for Haml Views | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | For a standard View class: | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | script/create.pl view HTML Haml | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | You can also pass parameters at will: | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | script/create.pl view HTML Haml vars_as_subs=1 format=html5 | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | This is a helper module for Haml Views. It is not meant to be used | ||||||||||||||||||
| 23 | directly. Instead, you should use your Catalyst app's "create" script | ||||||||||||||||||
| 24 | (see the SYNOPSIS for syntax). | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | =head2 Arguments | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | As any other view helper, the first argument is your View's name. In the | ||||||||||||||||||
| 29 | synopsys example we used "HTML", and it's usually a good name :) | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | The Haml helper accepts the same construction arguments as | ||||||||||||||||||
| 32 | L<Text::Haml itself|Text::Haml>. | ||||||||||||||||||
| 33 | List arguments can be separated by comma: | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | script/create.pl view HTML Haml path=/var/templates,/templates | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head2 METHODS | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =head3 mk_compclass | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | This method is used by the Catalyst helper engine to generate files properly. | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | =cut | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 0 | ▶ | sub mk_compclass { | ||||||||||||||||
|
Subroutine
mk_compclass: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 47 | 0 | my ( $self, $helper, @args ) = @_; | |||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 0 | my $file = $helper->{file}; | |||||||||||||||||
| 50 | 0 | my $template = 'compclass'; | |||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 0 | ▶ | if ( @args ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 53 | 0 | $helper->{loader_args} = _build_strings(_parse_args(@args)); | |||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 0 | $helper->render_file( $template, $file ); | |||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 1 | ▶ | sub _parse_args { | ||||||||||||||||
|
Subroutine
_parse_args: called CC=4 SCAR=14.1
| |||||||||||||||||||
| 60 | 1 | my $args = {}; | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 1 | my %need_array = map { $_, 1 } qw(path); | |||||||||||||||||
| 63 | 0 | my %need_hash = map { $_, 1 } qw(); #TODO | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 1 | foreach my $item (@_) { | |||||||||||||||||
| 66 | 3 | my ($key, $value) = split /=(?![>])/, $item; | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 3 | ▶ | if ( exists $need_array{$key} ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 69 | 1 | push @{ $args->{$key} }, split /,/, $value; | |||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | elsif (exists $need_hash{$key} ) { | ||||||||||||||||||
| 72 | 0 | $args->{$key}->{'data'} = $value; | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | else { | ||||||||||||||||||
| 75 | 2 | $args->{$key} = $value; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | 1 | return $args; | |||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | 1 | ▶ | sub _build_strings { | ||||||||||||||||
|
Subroutine
_build_strings: called CC=5 SCAR=16.3
| |||||||||||||||||||
| 82 | 1 | my $args = shift; | |||||||||||||||||
| 83 | 1 | my $return = {}; | |||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 1 | foreach my $key (keys %$args) { | |||||||||||||||||
| 86 | 3 | my $ref = ref $args->{$key}; | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 3 | my $value = ''; | |||||||||||||||||
| 89 | 3 | ▶ | if (!$ref) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 90 | 2 | $value = q[ default => '] . $args->{$key} . q[' ]; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | elsif ($ref eq 'HASH') { | ||||||||||||||||||
| 93 | $value = $/ | ||||||||||||||||||
| 94 | . ' default => sub { { ' | ||||||||||||||||||
| 95 | 0 | . $args->{$key}->{'data'} | |||||||||||||||||
| 96 | . ' } }' | ||||||||||||||||||
| 97 | . $/ | ||||||||||||||||||
| 98 | ; | ||||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | elsif ($ref eq 'ARRAY') { | ||||||||||||||||||
| 101 | $value = $/ | ||||||||||||||||||
| 102 | . ' default => sub { [ ' | ||||||||||||||||||
| 103 | 1 | . (join ', ', map { "'$_'" } @{$args->{$key}} ) | |||||||||||||||||
| 104 | . ' ] }' | ||||||||||||||||||
| 105 | . $/ | ||||||||||||||||||
| 106 | ; | ||||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | 3 | $return->{$key} = $value; | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | 1 | return $return; | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | =head1 SEE ALSO | ||||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | L<Catalyst::View::Haml>, L<Catalyst::Manual>, L<Catalyst::Helper>, | ||||||||||||||||||
| 116 | L<Text::Haml>. | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | Breno G. de Oliveira C<< <garu@cpan.org> >> | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | =head1 LICENSE | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | This library is free software . You can redistribute it and/or modify | ||||||||||||||||||
| 125 | it under the same terms as perl itself. | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | =cut | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 1 | 1; | |||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | __DATA__ | ||||||||||||||||||