Difference between revisions of "TemplateSnippets"

From Request Tracker Wiki
Jump to navigation Jump to search
(One intermediate revision by the same user not shown)
Line 2: Line 2:


This page introduce you with some code that can used in notification [[Template]]s and is part of [[CodeSnippets]] series of articles.
This page introduce you with some code that can used in notification [[Template]]s 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,<span style="font-size:13px;"> 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 </span><span style="font-size:13px;">look in RT__Ticket/ColumnMap for the DueRelative sub. (From Kevin Falcone)</span>


= Notification templates specifics =
= Notification templates specifics =
Line 18: Line 20:


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


That's it. Enjoy templating.
That's it. Enjoy templating.
Line 80: Line 82:


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


== Dates ==
== Dates ==
Line 114: Line 116:
     $date-&gt;AsString;
     $date-&gt;AsString;
  }
  }
=== Relative dates===
=== Relative dates===


You often want to show a ''relative'' ("friendly" or "humanized") date, ie "due in 35 minutes". This code can be inserted to define a package variable $relativedue that can be substituted to insert the relative due date. 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.<span style="font-size:13px;"> </span>
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.
 
<p style="margin-top:0.4em;margin-bottom:0.5em;">Code to set $relativedate:</p>
{
    my $d = RT::Date->new(RT::SystemUser);
    $d->Set(Format=>'ISO', Value=>$Ticket->Due, Timezone=>"GMT");
    our $relativedue = $d->AgeAsString();
    '';''
}


<span style="font-size:13px;">$relativedue is now defined as a package variable in the template that can be referred to anywhere after with a simple:</span>
  { $Ticket->DueObj->AgeAsString() }


<span style="font-size:13px;">{our $relativedate;}</span>
This will work fine in templates invoked with rt-crontool.
This will work fine in templates invoked with rt-crontool.


<span style="font-size:18px;">Complex</span>
== Complex ==


Add the number of tickets in queue:
Add the number of tickets in queue:

Revision as of 04:04, 20 February 2013

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