From: Mark Addison Date: Fri, 14 May 2004 03:08:15 +0000 (+0000) Subject: Initial stab at a section on building producers with TT::Base X-Git-Tag: v0.06~54 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=08a6f829919706c98655fc6194b5113d931f8e13;p=dbsrgits%2FSQL-Translator.git Initial stab at a section on building producers with TT::Base --- diff --git a/lib/SQL/Translator/Manual.pod b/lib/SQL/Translator/Manual.pod index 554e33f..14b5b22 100644 --- a/lib/SQL/Translator/Manual.pod +++ b/lib/SQL/Translator/Manual.pod @@ -469,6 +469,77 @@ And here is the output produced by this script: If you create a useful parser or producer, you are encouraged to submit your work to the SQLFairy project! +=head1 PLUGIN TEMPLATE TOOLKIT PRODUCERS + +You may find that the TTSchema producer doesn't give you enough control over +templating and you want to play with the Template config or add you own +variables. Or maybe you just have a really good template you want to submit to +SQLFairy :) If so, the SQL::Translator::Producer::TT::Base producer may be +just for you! Instead of working like a normal producer it provides a base +class so you can cheaply build new producer modules based on templates. + +It's simplest use is when we just want to put a single template in its own +module. So to create a Foo producer we create a F file as +follows, putting our template in the __DATA__ section. + + package Custom::Foo.pm; + use base qw/SQL::Translator::Producer::TT::Base/; + # Use our new class as the producer + sub produce { return __PACKAGE__->new( translator => shift )->run; }; + + __DATA__ + [% FOREACH table IN schema.get_tables %] + Table: [% table.name %] + Fields: + [% FOREACH field IN table.get_fields -%] + [% field.name %] + [% END -%] + [% END %] + +For that we get a producer called Custom::Foo that we can now call like a +normal producer (as long as the directory with F is in our @INC +path): + + $ sqlt -f YAML -t Custom-Foo foo.yaml + +The template gets variables of C and C to use in building +its output. You also get a number of methods you can override to hook into the +template generation. + +B Allows you to set the config options used by the Template object. +The Template Toolkit provides a huge number of options which allow you to do all +sorts of magic (See L for details). This method +provides a hook into them by returning a hash of options for the Template. e.g. +Say you want to use the INTERPOLATE option to save some typing in your template; + + sub tt_config { ( INTERPOLATE => 1 ); } + +A common use for this is to add you own filters to the template: + + sub tt_config {( + INTERPOLATE => 1, + FILTERS => { foo_filter => \&foo_filter, } + );} + +Another common extension is adding your own template variables. This is done +with B: + + sub tt_vars { ( foo => "bar" ); } + +What about using template files instead of DATA sections? You can already! if +you give a template on the command line your new producer will use that instead +of reading the DATA section: + + $ sqlt -f YAML -t Custom-Foo --template foo.tt foo.yaml + +This is usefull as you can set up a producer that adds a set of filters and +variables that you can then use in templates given on the command line. (There +is also a tt_schema method to over ride if you need even finer control over the +source of your template). Note that if you leave out the DATA section all +together then your producer will require a template file name to be given. + +See L for more details of what you can do. + =head1 AUTHOR Ken Y. Clark Ekclark@cpan.orgE.