From: Mark Addison Date: Tue, 16 Nov 2004 21:06:35 +0000 (+0000) Subject: Re-added ttvars. Added docs and test for ttvars. X-Git-Tag: v0.11008~596 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=046d668a030d767eb367fe2045a395d7bffd482b;p=dbsrgits%2FSQL-Translator.git Re-added ttvars. Added docs and test for ttvars. --- diff --git a/lib/SQL/Translator/Producer/TTSchema.pm b/lib/SQL/Translator/Producer/TTSchema.pm index 29b1a9a..89f77ca 100644 --- a/lib/SQL/Translator/Producer/TTSchema.pm +++ b/lib/SQL/Translator/Producer/TTSchema.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::TTSchema; # ------------------------------------------------------------------- -# $Id: TTSchema.pm,v 1.7 2004-11-16 09:15:36 boconnor Exp $ +# $Id: TTSchema.pm,v 1.8 2004-11-16 21:06:35 grommit Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -24,7 +24,7 @@ package SQL::Translator::Producer::TTSchema; =head1 NAME -SQL::Translator::Producer::TTSchema - +SQL::Translator::Producer::TTSchema - Produces output using the Template Toolkit from a SQL schema =head1 SYNOPSIS @@ -35,8 +35,12 @@ SQL::Translator::Producer::TTSchema - filename => 'foo_schema.sql', to => 'TTSchema', producer_args => { - ttargs => {}, - ttfile => 'foo_template.tt', + ttfile => 'foo_template.tt', # Template file to use + + # Extra template variables + ttargs => { + author => "Mr Foo", + }, }, ); print $translator->translate; @@ -46,10 +50,10 @@ SQL::Translator::Producer::TTSchema - Produces schema output using a given Template Tookit template. It needs one additional producer_arg of C which is the file -name of the template to use. This template will be passed a single -argument called C, which is the -C object, which you can then use to -walk the schema via the methods documented in that module. +name of the template to use. This template will be passed a variable +called C, which is the C object +created by the parser. You can then use it to walk the schema via the +methods documented in that module. Here's a brief example of what the template could look like: @@ -65,7 +69,10 @@ Here's a brief example of what the template could look like: See F for a more complete example. -You can also set any of the options used to initiallize the Template object by +The template will also get the set of extra variables given as a hashref via the +C producer arg. + +You can set any of the options used to initiallize the Template object by adding them to your producer_args. See Template Toolkit docs for details of the options. @@ -86,6 +93,20 @@ schema into MySQL's syntax, your own HTML documentation, your own Class::DBI classes (or some other code) -- the opportunities are limitless! +=head2 Producer Args + +=over 4 + +=item ttfile + +The template file to generate the output with. + +=item ttvars + +A hash ref of extra variables you want to add to the template. + +=back + =cut # ------------------------------------------------------------------- @@ -93,7 +114,7 @@ limitless! use strict; use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/; $DEBUG = 0 unless defined $DEBUG; use Template; @@ -110,7 +131,9 @@ sub produce { my $scma = $translator->schema; my $args = $translator->producer_args; my $file = delete $args->{'ttfile'} or die "No template file!"; - + my $ttvars = delete $args->{'ttargs'} || {}; + # Any args left here get given to the Template object. + debug "Processing template $file\n"; my $out; my $tt = Template->new( @@ -120,10 +143,10 @@ sub produce { %$args, # Allow any TT opts to be passed in the producer_args ) || die "Failed to initialize Template object: ".Template->error; - $tt->process( - $file, - { schema => $scma , %{ $args || {} } }, - \$out + $tt->process( + $file, + { schema => $scma , %$ttvars }, + \$out ) or die "Error processing template '$file': ".$tt->error; return $out; diff --git a/t/18ttschema-producer.t b/t/18ttschema-producer.t index 7c127d4..8bf46c6 100644 --- a/t/18ttschema-producer.t +++ b/t/18ttschema-producer.t @@ -41,8 +41,12 @@ $obj = SQL::Translator->new( from => "XML-SQLFairy", filename => "$Bin/data/xml/schema.xml", to => "TTSchema", - producer_args => { + producer_args => { ttfile => "$Bin/data/template/basic.tt", + ttargs => { + foo => 'bar', + hello => 'world', + }, }, ); my $out; @@ -58,6 +62,9 @@ __DATA__ Schema: Database: +Foo: bar +Hello: world + Table: Basic ========================================================================== diff --git a/t/data/template/basic.tt b/t/data/template/basic.tt index 8a0f1af..6011a1c 100644 --- a/t/data/template/basic.tt +++ b/t/data/template/basic.tt @@ -1,6 +1,9 @@ Schema: [% schema.name %] Database: [% schema.database %] +Foo: [% foo %] +Hello: [% hello %] + [%- FOREACH table = schema.get_tables %] Table: [% table %] ==========================================================================