TemplateSnippets: Difference between revisions
No edit summary |
(Document how to format relative dates in templates) |
||
Line 18: | Line 18: | ||
<nowiki>{ my $out = ''; | <nowiki>{ 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 | |||
} | |||
</nowiki> | |||
That's it. Enjoy templating. | That's it. Enjoy templating. | ||
Line 80: | Line 80: | ||
<nowiki>Requestors: { | <nowiki>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; | |||
} | |||
</nowiki> | |||
== Dates == | == Dates == | ||
Line 102: | Line 102: | ||
The same for Transaction | The same for Transaction | ||
{ $Transaction->CreatedAsString } | { $Transaction->CreatedAsString } | ||
Show the first date that is set: | <p style="margin-top:1em;margin-bottom:1em;font-size:13.333333015441895px;">Show the first date that is set:</p> | ||
This ticket was last active on { | This ticket was last active on { | ||
Line 115: | Line 114: | ||
$date->AsString; | $date->AsString; | ||
} | } | ||
===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> | |||
<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> | |||
<span style="font-size:13px;">{our $relativedate;}</span> | |||
This will work fine in templates invoked with rt-crontool. | |||
= | <span style="font-size:18px;">Complex</span> | ||
Add the number of tickets in queue: | Add the number of tickets in queue: |
Revision as of 05:01, 18 February 2013
Introduction
This page introduce you with some code that can used in notification Templates and is part of CodeSnippets series of articles.
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". 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.
Code to set $relativedate:
{ my $d = RT::Date->new(RT::SystemUser); $d->Set(Format=>'ISO', Value=>$Ticket->Due, Timezone=>"GMT"); our $relativedue = $d->AgeAsString(); ; }
$relativedue is now defined as a package variable in the template that can be referred to anywhere after with a simple:
{our $relativedate;}
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 } }