From Request Tracker Wiki
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;
  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 {

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