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 implementation 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
87 free.
88
89 tt_default_vars
90 Return a hash-ref of the default vars given to the template. You
91 wouldn't normally over-ride this, just inherit the default
92 implementation, to get the "translator" & "schema" variables, then
93 over-ride "tt_vars" to add your own.
94
95 The current default variables are:
96
97 schema
98 The schema to template.
99
100 translator
101 The SQL::Translator object.
102
103 pre_process_schema
104 WARNING: This method is Experimental so may change!
105
106 Called with the SQL::Translator::Schema object and should return one
107 (it doesn't have to be the same one) that will become the "schema"
108 variable used in the template.
109
110 Gets called from tt_default_vars.
111
113 The rest of the methods in the class set up a sub-classable producer
114 object. You normally just inherit them.
115
116 new
117 my $tt_producer = TT::Base->new( translator => $translator );
118
119 Construct a new TT Producer object. Takes a single, named arg of the
120 SQL::Translator object running the translation. Dies if this is not
121 given.
122
123 translator
124 Return the SQL::Translator object.
125
126 schema
127 Return the SQL::Translator::Schema we are translating. This is
128 equivalent to "$tt_producer->translator->schema".
129
130 run
131 Called to actually produce the output, calling the sub class hooks.
132 Returns the produced text.
133
134 args
135 Util wrapper method around "TT::Base->translator->producer_args" for
136 (mostly) readonly access to the producer args. How it works depends on
137 the number of arguments you give it and the context.
138
139 No args - Return hashref (the actual hash in Translator) or hash of args.
140 1 arg - Return value of the arg with the passed name.
141 2+ args - List of names. In list context returns values of the given arg
142 names, returns as a hashref in scalar context. Any names given
143 that don't exist in the args are returned as undef.
144
145 This is still a bit messy but is a handy way to access the producer
146 args when you use your own to drive the templating.
147
149 perl, SQL::Translator, Template.
150
152 - Add support for a sqlf template repository, set as an INCLUDE_PATH,
153 so that sub-classes can easily include file based templates using
154 relative paths.
155
156 - Pass in template vars from the producer args and command line.
157
158 - Merge in TT::Table.
159
160 - Hooks to pre-process the schema and post-process the output.
161
163 Mark Addison <grommit@users.sourceforge.net>.
164
165
166
167perl v5.30.1 2020-01S-Q3L0::Translator::Producer::TT::Base(3)