Difference between revisions of "TemplateSnippets"

From Request Tracker Wiki
Jump to navigation Jump to search
 
m (8 revisions imported)

Revision as of 15:39, 6 April 2016

Introduction

This page introduce you with some code that can used in notification Templates and is part of CodeSnippets series of articles.

If you want to do something that RT does in its user interface that isn't listed here, you can take a quick look at share/html/Elements/*/ColumnMap . For example, if looking for how to create a relative due date, you'd look in RT__Ticket/ColumnMap for the DueRelative sub. (From Kevin Falcone)

Notification templates specifics

See also Template that describe why this text is about notifications and how can it be different for other actions.

In notification templates you have to enclose code into curly brackets:

{ ... here goes some code ... }

Ticket object is stored in $Ticket variable and transaction is stored in $Transaction, for example:

{ $Ticket->Status }

Each block of should result in some text (may be empty). In a simple case you just add code that results in text, like with status of a ticket above. For complex code use some local variable and append data to it:

{ my $out = '';
        $out .= "append some text line\n";
        foreach my $element (...) {
            $out .= "append more data $element\n";
        }
        $out; # don't forget to put $out at the end to return it
      }
      
      

That's it. Enjoy templating.

Code snippets

Basics

Ticket status

{ $Ticket->Status }

Queue name

{ $Ticket->QueueObj->Name }

Insert Queue email address for correspondence/comment

{ $Ticket->QueueObj->CorrespondAddress; }
{ $Ticket->QueueObj->CommentAddress; }

Puts content of the first transaction into email

{ $Ticket->Transactions->First->Content }

People

Get the email address of the user who created the ticket

{ $Ticket->CreatorObj->EmailAddress }

Get the email address of the user who created the transaction

{ $Transaction->CreatorObj->EmailAddress }

Get the name of the user who created the transaction

{ $Transaction->CreatorObj->Name }

Get the name of the user who last updated the ticket

{ $Ticket->LastUpdatedByObj->Name }

Get the email addresses of the Requestor(s)

{ $Ticket->RequestorAddresses }

Get the email addresses of the users added as CC or AdminCC to the ticket

{ $Ticket->CcAddresses }
{ $Ticket->AdminCcAddresses }

Requestors' names or other properties

Requestors: {
        # 'Requestors' may be replaced with 'Cc' or 'AdminCc'
        my $users = $Ticket->Requestors->UserMembersObj;
        my $output = '';
        while( my $user = $users->Next ) {
           $output .= ', ' if $output; # comma between values
           $output .= $user->Name; # or any other user's property
        }
        $output;
      }
      

Dates

Ticket creation time as formatted string:

{ $Ticket->CreatedAsString }

The same:

{ $Ticket->CreatedObj->AsString }

The same for Transaction

{ $Transaction->CreatedAsString }

Show the first date that is set:

This ticket was last active on {
    my $date;
    foreach my $method ( qw(ToldObj LastUpdatedObj CreatedObj) ) {
        $date = $Ticket->$method(); # get date
        last if $date->Unix > 0; # stop on first that is set
    }
    $date->AsString;
}

 Relative dates

You often want to show a relative ("friendly" or "humanized") date, ie "due in 35 minutes". The same approach will work for any of the $Ticket object's other dates, like Start, too. It and will append "ago" where the date is in the past.

 { $Ticket->DueObj->AgeAsString() }

This will work fine in templates invoked with rt-crontool.

Complex

Add the number of tickets in queue:

There are currently {
  my $tickets = RT::Tickets->new( $RT::SystemUser );
  $tickets->FromSQL( "Status = 'new' AND Queue = ". $Ticket->Queue );
  $tickets->Count;
} ticket(s) awaiting processing before yours, please be patient.

Tips&Tricks

Don't show content if it's empty

{ if( my $tcc = $Ticket->CcAddresses ) { "Ticket Ccs: ". $tcc } }
{ if( my $qcc = $Ticket->QueueObj->CcAddresses ) { "Queue Ccs: ". $qcc } }
{ if( my $tacc = $Ticket->AdminCcAddresses ) { "Ticket AdminCcs: ". $tacc } }

See also

Template, CodeSnippets, CustomConditionSnippets