ForkTemplate

From Request Tracker Wiki
Revision as of 04:37, 25 June 2012 by 80.152.250.252 (talk) (changed code to evaluate perl and simple templates)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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;
     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 ? $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' );