App-Templer

 view release on metacpan or  search on metacpan

lib/Templer/Plugin/FileContents.pm  view on Meta::CPAN


                #
                #  Relative file.  We want to look for the file based upon the
                # global include-path with the current directory appended.
                #
                #  In the case of multiple matches the last one
                # wins.  i.e. The non-global one.
                #
                #

                #
                #  Get the global variables from the configuration object.
                #
                my $dirs = $site->get("include-path");
                my @dirs = @$dirs;

                #
                #  Append the directory from the input page.
                #
                push( @dirs, File::Basename::dirname( $page->source() ) );

                #
                #  Iterate
                #
                foreach my $dir (@dirs)
                {
                    if ( -e $dir . "/" . $file )
                    {
                        $file = $dir . "/" . $file;
                    }
                }
            }

            $hash{ $key } = $self->file_contents($file);

            #
            #  The page dependency also includes the filename now.
            #
            $page->add_dependency($file);
        }
    }

    return ( \%hash );
}


#
#  Return the contents of the named file.
#
sub file_contents
{
    my ( $self, $name ) = (@_);

    my $content = "";

    if ( -e $name )
    {
        open( my $handle, "<:utf8", $name ) or
          return "";

        binmode( $handle, ":utf8" );

        while ( my $line = <$handle> )
        {
            $content .= $line;
        }
        close($handle);
    }
    else
    {
        print "WARNING: Attempting to read a file that doesn't exist: $name\n";
    }

    $content;
}

#
#  Register the plugin.
#
Templer::Plugin::Factory->new()
  ->register_plugin("Templer::Plugin::FileContents");



( run in 0.941 second using v1.01-cache-2.11-cpan-39bf76dae61 )