1SQL::Translator::ProducUesre:r:TCTo:n:tBraisbeu(t3e)d PeSrQlL:D:oTcruamnesnltaattoiro:n:Producer::TT::Base(3)
2
3
4
6 SQL::Translator::Producer::TT::Base - TT (Template Toolkit) based
7 Producer base class.
8
10 # Create a producer using a template in the __DATA__ section.
11 package SQL::Translator::Producer::Foo;
12
13 use base qw/SQL::Translator::Producer::TT::Base/;
14
15 # Convert produce call into a method call on our new class
16 sub produce { return __PACKAGE__->new( translator => shift )->run; };
17
18 # Configure the Template object.
19 sub tt_config { ( INTERPOLATE => 1 ); }
20
21 # Extra vars to add to the template
22 sub tt_vars { ( foo => "bar" ); }
23
24 # Put template in DATA section (or use file with ttfile producer arg)
25 __DATA__
26 Schema
27
28 Database: [% schema.database %]
29 Foo: $foo
30 ...
31
33 A base class producer designed to be sub-classed to create new TT based
34 producers cheaply - by simply giving the template to use and sprinkling
35 in some extra template variables and config.
36
37 You can find an introduction to this module in SQL::Translator::Manual.
38
39 The 1st thing the module does is convert the produce sub routine call
40 we get from SQL::Translator into a method call on an object, which we
41 can then sub-class. This is done with the following code which needs to
42 appear in all sub classes.
43
44 # Convert produce call into an object method call
45 sub produce { return __PACKAGE__->new( translator => shift )->run; };
46
47 See "PRODUCER OBJECT" below for details.
48
49 The upshot of this is we can make new template producers by sub
50 classing this base class, adding the above snippet and a template. The
51 module also provides a number of hooks into the templating process, see
52 "SUB CLASS HOOKS" for details.
53
54 See the SYNOPSIS above for an example of creating a simple producer
55 using a single template stored in the producers DATA section.
56
58 Sub-classes can override these methods to control the templating by
59 giving the template source, adding variables and giving config to the
60 Tempate object.
61
62 tt_config
63 sub tt_config { ( INTERPOLATE => 1 ); }
64
65 Return hash of Template config to add to that given to the Template
66 "new" method.
67
68 tt_schema
69 sub tt_schema { "foo.tt"; }
70 sub tt_schema { local $/ = undef; \<DATA>; }
71
72 The template to use, return a file name or a scalar ref of TT source,
73 or an IO::Handle. See Template for details, as the return from this is
74 passed on to it's "produce" method.
75
76 The default implimentation uses the producer arg "ttfile" as a filename
77 to read the template from. If the arg isn't there it will look for a
78 "__DATA__" section in the class, reading it as template source if
79 found. Returns undef if both these fail, causing the produce call to
80 fail with a 'no template!' error.
81
82 tt_vars
83 sub tt_vars { ( foo => "bar" ); }
84
85 Return hash of template vars to use in the template. Nothing added here
86 by default, but see tt_default_vars for the variables you get for free.
87
88 tt_default_vars
89 Return a hash-ref of the default vars given to the template. You
90 wouldn't normally over-ride this, just inherit the default
91 implimentation, to get the "translator" & "schema" variables, then
92 over-ride tt_vars to add your own.
93
94 The current default variables are:
95
96 schema
97 The schema to template.
98
99 translator
100 The SQL::Translator object.
101
102 pre_process_schema
103 WARNING: This method is Experimental so may change!
104
105 Called with the SQL::Translator::Schema object and should return one
106 (it doesn't have to be the same one) that will become the "schema"
107 varibale used in the template.
108
109 Gets called from tt_default_vars.
110
112 The rest of the methods in the class set up a sub-classable producer
113 object. You normally just inherit them.
114
115 new
116 my $tt_producer = TT::Base->new( translator => $translator );
117
118 Construct a new TT Producer object. Takes a single, named arg of the
119 SQL::Translator object running the translation. Dies if this is not
120 given.
121
122 translator
123 Return the SQL::Translator object.
124
125 schema
126 Return the SQL::Translator::Schema we are translating. This is
127 equivilent to "$tt_producer->translator->schema".
128
129 run
130 Called to actually produce the output, calling the sub class hooks.
131 Returns the produced text.
132
133 args
134 Util wrapper method around "TT::Base->translator->producer_args" for
135 (mostley) readonly access to the producer args. How it works depends on
136 the number of arguments you give it and the context.
137
138 No args - Return hashref (the actual hash in Translator) or hash of args.
139 1 arg - Return value of the arg with the passed name.
140 2+ args - List of names. In list context returns values of the given arg
141 names, returns as a hashref in scalar context. Any names given
142 that don't exist in the args are returned as undef.
143
144 This is still a bit messy but is a handy way to access the producer
145 args when you use your own to drive the templating.
146
148 perl, SQL::Translator, Template.
149
151 - Add support for a sqlf template repository, set as an INCLUDE_PATH,
152 so that sub-classes can easily include file based templates using
153 relative paths.
154
155 - Pass in template vars from the producer args and command line.
156
157 - Merge in TT::Table.
158
159 - Hooks to pre-process the schema and post-process the output.
160
162 Mark Addison <grommit@users.sourceforge.net>.
163
164
165
166perl v5.12.0 2009-08S-Q1L8::Translator::Producer::TT::Base(3)