| Template-AsGraph documentation | Contained in the Template-AsGraph distribution. |
Template::AsGraph - Create a graph from a Template Toolkit file
Version 0.01
Given a Template Toolkit filename, it generates a Graph::Easy object with the entire template flow.
use Template::AsGraph;
# quick graph creation for any TT2 template
my $graph = Template::AsGraph->graph('mytemplate.tt2');
You can also set up any TT configurations such as different tag styles and INCLUDE_PATHs. You can even set OUTPUT to actually get the processed template (see documentation below):
my %config = (
INCLUDE_PATH => 'root/src/',
START_TAG => '<+',
END_TAG => '+>',
PLUGIN_BASE => 'MyApp::Template::Plugin',
PRE_PROCESS => 'header',
OUTPUT => \$output,
);
my $graph = Template::AsGraph->graph('mytemplate.tt2', \%config);
Alternatively, if you have dinamically loaded templates, you may want to pass on variables and other TT options just as you would to a regular Template object.
my $graph = Template::AsGraph->graph('mytemplate.tt2', \%config, \%vars);
The returnerd $graph is a Graph::Easy object, so you can manipulate it at will. For example, save as a PNG file (assuming you have graphviz's "dot" binary)
if (open my $png, '|-', 'dot -Tpng -o routes.png') {
print $png $graph->as_graphviz;
close($png);
}
Receives a template name and generates a Graph::Easy object with a representation of the template's flow. It may optionally receive any Template configuration option and variables.
Breno G. de Oliveira, <garu at cpan.org>
Although this module should work without any quirks and DWIM for almost everyone, there are some minor issues that can emerge with advanced users:
CONTEXT => My::Custom::Context->new(),
Please report any bugs or feature requests to bug-template-asgraph at rt.cpan.org, or through
the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-AsGraph. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Template::AsGraph
You can also look for information at:
Andy Wardley and his awesome Template Toolkit deserve all the praise. Also, many thanks to Pedro Melo and his MojoX::Routes::AsGraph, which served as inspiration for this module from the main idea down to the actual code.
Copyright 2009 Breno G. de Oliveira, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Template-AsGraph documentation | Contained in the Template-AsGraph distribution. |
package Template::AsGraph; use warnings; use strict; use Graph::Easy; use Template; use Carp 'croak'; use File::Spec; our $VERSION = '0.01'; sub graph { # get parameters my $self = shift; my $filename = shift or croak "you must specify a template name"; my $config = (@_ ? shift : {}); my $vars = (@_ ? shift : {}); unless (exists $config->{OUTPUT}) { $config->{OUTPUT} ||= File::Spec->devnull; } # setup our own context object. This can be # overridable by user's $config, assuming # they know what they're doing $Template::Config::CONTEXT = 'Template::AsGraph::Context'; # process the given template, to populate # context's tree structure my $template = Template->new($config); $template->process($filename, $vars) || croak $template->error; # grab our shiny tree and make it a graph! my $tree = $template->context->tree; my $graph = Graph::Easy->new(); foreach my $child (keys %{$tree}) { _new_node($graph, $filename, $tree->{$child}); } return $graph; } # this internal method recursively fills # our graph with appropriate node values sub _new_node { my ($graph, $name, $tree) = (@_); # add current node to graph my $node = $graph->add_node($name); # link each child to it foreach my $child (keys %{$tree}) { my $child_node = _new_node($graph, $child, $tree->{$child}); $graph->add_edge($node, $child_node); } return $node; } 42; __END__