Difference between revisions of "ForkTemplate"

From Request Tracker Wiki
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
    Optional common intro
 
   
  {
    {
    use RT::Template;
      use RT::Template;
    my $selector = $Ticket-&gt;FirstCustomFieldValue('selector');
      use Encode;
    my $obj = new RT::Template($RT::SystemUser);
      my $selector = $Ticket-&gt;FirstCustomFieldValue('selector');
    $obj-&gt;Load( $selector &lt; 5 ? 'Template A' : 'Template B');
      my $obj = new RT::Template($RT::SystemUser);
 
      $obj-&gt;Load( $selector &lt; 5 ? 'Template A' : 'Template B');
    if ($obj->Type == 'Perl') {
   
        my($ret, $msg) = $obj-&gt;Parse();
      if ($obj->Type == 'Perl') {
        $ret ? $obj-&gt;MIMEObj-&gt;stringify_body : $msg;
        my($ret, $msg) = $obj->Parse();
    } else {
        $ret ? Encode::decode( 'utf-8', $obj->MIMEObj->stringify_body) : $msg;
        $obj-&gt;Content();
      } else {
    }
        $obj->Content();
  }
      }
 
    }
  Optional common outro
   
 
    Optional common outro
  </nowiki>
   
    </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-&gt;[[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-&gt;[[SetTemplate]] in [[CustomCondition]].

Latest revision as of 16: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' );