ForkTemplate: Difference between revisions
Jump to navigation
Jump to search
(changed code to evaluate perl and simple templates) |
m (4 revisions imported) |
||
(One intermediate revision by one other user not shown) | |||
Line 2: | Line 2: | ||
<nowiki>{my $dummy="Reminder to include a blank line above your actual text if not including any headers";} | <nowiki>{my $dummy="Reminder to include a blank line above your actual text if not including any headers";} | ||
Optional common intro | |||
{ | |||
use RT::Template; | |||
use Encode; | |||
my $selector = $Ticket->FirstCustomFieldValue('selector'); | |||
my $obj = new RT::Template($RT::SystemUser); | |||
$obj->Load( $selector < 5 ? 'Template A' : 'Template B'); | |||
if ($obj->Type == 'Perl') { | |||
my($ret, $msg) = $obj->Parse(); | |||
$ret ? Encode::decode( 'utf-8', $obj->MIMEObj->stringify_body) : $msg; | |||
} else { | |||
$obj->Content(); | |||
} | |||
} | |||
Optional common outro | |||
</nowiki> | |||
This will inject the contents of another template into the current one. It's an easy way to provide some common and some specific content in a response, or to create a template which acts as a switch without having to create separate scrips for each condition. The latter effect could also be achieved through the clever use of $self->[[SetTemplate]] in [[CustomCondition]]. | This will inject the contents of another template into the current one. It's an easy way to provide some common and some specific content in a response, or to create a template which acts as a switch without having to create separate scrips for each condition. The latter effect could also be achieved through the clever use of $self->[[SetTemplate]] in [[CustomCondition]]. |
Latest revision as of 15:09, 6 April 2016
Here's a little chunk of code you can use in a template to send different responses depending upon a condition of your choice, without hardcoding your variants into the code:
{my $dummy="Reminder to include a blank line above your actual text if not including any headers";} Optional common intro { use RT::Template; use Encode; my $selector = $Ticket->FirstCustomFieldValue('selector'); my $obj = new RT::Template($RT::SystemUser); $obj->Load( $selector < 5 ? 'Template A' : 'Template B'); if ($obj->Type == 'Perl') { my($ret, $msg) = $obj->Parse(); $ret ? Encode::decode( 'utf-8', $obj->MIMEObj->stringify_body) : $msg; } else { $obj->Content(); } } Optional common outro
This will inject the contents of another template into the current one. It's an easy way to provide some common and some specific content in a response, or to create a template which acts as a switch without having to create separate scrips for each condition. The latter effect could also be achieved through the clever use of $self->SetTemplate in CustomCondition.
my $trans = $self->TransactionObj; return 0 unless $trans->Type eq "Create"; $self->SetTemplate( $condition ? 'foo' : 'bar' );