View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Chris Winters > OpenInteract-1.62 > OpenInteract::Template::Plugin



Annotate this POD

View/Report Bugs
Module Version: 1.32   Source  


OpenInteract::Template::Plugin - Custom OpenInteract functionality in templates


 # Create the TT object with the OI plugin
 my $template = Template->new(
                       PLUGINS => { OI => 'OpenInteract::Template::Plugin' }, ... );
 my ( $output );
 $template->process( 'package::template', \%params, \$output );
 # In the template (brief examples, see below for more)
 [% OI.show_all_actions.join( "\n" ) -%]
 [% OI.comp( 'error_display', error_msg = error_msg ) -%]
 [% OI.box_add( 'contact_tools_box', title  = 'Contact Tools',
                                     weight = 2 ) -%]
 [% object_info = OI.object_description( object ) %]
 This is a [% %] object.
 Is the object in the class? 
    [% OI.class_isa( object, 'SPOPS::DBI' ) ? 'yes' : 'no' %]
 Today is [% OI.date_format( 'now', '%Y-%m-%e %l:%M %p' ) %] the
 [% OI.date_format( 'now', '%j' ) %] day of the year
 [% d = OI.date_into_object( object.updated_on ) -%]
 [% OI.comp( 'date_select', month_value  = d.month,
                            day_value    =,
                            year_value   = d.year, blank = 1,
                            field_prefix = 'updated_on' ) -%]
 [% OI.limit_string( object.description, 30 ) %]
 var person_last_name = '[% OI.javascript_quote( person.last_name ) %]';
 [% matched = OI.regex_chunk( 'It was the best of times, it was the blurst of times.',
                              '(blurst.*)' ) %]
 [% OI.limit_sentences( news.news_item, 3 ) %]
 [% score = grade.score / %]
 Your grade is: [% OI.percent_format( score ) %]
 You have [% OI.money_format( account.balance ) %] left to spend.
 Hello [% OI.uc_first( person.first_name ) %]
 <textarea name="news_item">[% OI.html_encode( news.news_item ) %]</textarea>
 Item: [% OI.html_decode( news.news_item ) %]
 [% edit_url = OI.make_url( base = '/User/show/', user_id = OI.login.user_id,
                            edit = 1, show_all = 'yes' ) %]
 <a href="[% edit_url %]">Edit your information</a>
 [% theme = OI.theme_properties %]
 Background color of page: [% theme.bgcolor %]
 [% new_theme = OI.theme_fetch( 5 ) %]
 Background color of page from other theme: [% new_theme.bgcolor %]
 [% IF OI.logged_in -%]
 Hello [% OI.login.full_name %]. 
   Your groups are: [% OI.login_group.join( ', ' ) -%]
 [% ELSE -%]
 You are not logged in.
 [% END -%]
 Your last search: [% OI.session.latest_search %]
 <a href="[% OI.return_url %]">Refresh</a>
 [% IF OI.error_hold.myapp.field_out_of_bounds %]
   The entry you made is out of bounds:
        [% OI.error_hold.myapp.field_out_of_bounds %]
 [% END %]
 [% IF object.tmp_security_level >= OI.security_level.write -%]
   you can edit this object!
 [% END %]


This implements a Template Toolkit Plugin. For more information about plugins, see Template::Manual::Plugins.

Normally a plugin is instantiated like this:

 [% USE OI %]
 [% object_info = OI.object_description( object ) %]

But since this plugin will probably be used quite a bit by OpenInteract template authors, it is always already created for you if you use the OpenInteract::Template::Process module.

It can be used outside of the normal OpenInteract processing by doing something like:

    my $template = Template->new(
                      PLUGINS => { OI => 'OpenInteract::Template::Plugin' }
    $template->process( $text, { OI => $template->context->plugin( 'OI' ) } )
         || die "Cannot process template! ", $template->error();

This is done for you in OpenInteract::Template::Process so you can simply do:

    my $R = OpenInteract::Startup->setup_static_environment_options();
    OpenInteract::Template::Process->initialize( $R->CONFIG );
    print OpenInteract::Template::Process->handler( {},
                                                    { foo => 'bar' },
                                                    { name => 'mypkg::mytemplate' });

And everything works. (See OpenInteract::Template::Process for more information.)

Most of the interesting information is in "METHODS AND PROPERTIES".


The following OpenInteract properties and methods are available through this plugin, so this describes how you can interface with OpenInteract from a template.



You can get a listing of all actions available via the plugin by doing:

 [% actions = OI.show_all_actions -%]
 [% actions.join( "\n" ) %]


comp( $name, \%params )

Calls the component processor and returns the output. Components can be very simple means for generating reusing HTML, or they can be complex data display/manipulation schemes.


 [% OI.comp( 'error_display', error_msg = error_msg ) %]

See OpenInteract::Component module for more information about components.

box_add( $box, \%params )

Adds a box to the list of boxes that will be processed by the 'boxes' component. (This is usually found in the 'base_main' template for your site.) You can add just a simple box name or parameters for the box as well. See the 'base_box' package for more information about boxes.


 [% OI.box_add( 'object_modify_box', object = news ) %]

 [% OI.box_add( 'object_modify_box', object = news, title = 'Change it!',
                                     weight = 1 ) %]

object_description( $spops_object )

Returns a hashref with metadata about any SPOPS object. Keys of the hashref are class, object_id (and oid), id_field, name, title, url, url_edit. (See SPOPS for details about what is returned.)

 [% desc = OI.object_description( news ) %]
 [% IF news.tmp_security_level >= OI.security_level.write %]
   <a href="[% desc.url_edit %]">Edit</a>
 [% END %]

class_isa( $class|$object, $isa_class )

Returns a true value if $class or $object is a $isa_class.


 [% IF OI.class_isa( news, 'MySite::NewsCustom' ) %]
   [% news.display_custom_news() %]
 [% ELSE %]
   [% news.display_news() %]
 [% END %]

(Of course, this is a bad example since you would deal with this through your normal OO methods.)

date_format( $date_string[, $format ] )

Formats the date from string $string using the strftime format $format. If you do not supply $format, a default of

 %Y-%m-%e %l:%M %p

is used.


  [% mydate = '2000-5-1 5:45 PM' %]
  Date [% mydate %] is day number [% OI.date_format( mydate, '%j' ) %] of the year.


  Date 2000-5-1 5:45 PM is day number 122 of the year.


  Today is day number [% OI.date_format( 'now', '%j' ) %] of the year.


  Today is day number 206 of the year.

For reference, here are strftime formatting sequences (cribbed from Date::Format):

  %%      PERCENT
  %a      day of the week abbr
  %A      day of the week
  %b      month abbr
  %B      month
  %c      MM/DD/YY HH:MM:SS
  %C      ctime format: Sat Nov 19 21:05:57 1994
  %d      numeric day of the month, with leading zeros (eg 01..31)
  %e      numeric day of the month, without leading zeros (eg 1..31)
  %D      MM/DD/YY
  %h      month abbr
  %H      hour, 24 hour clock, leading 0)
  %I      hour, 12 hour clock, leading 0)
  %j      day of the year
  %k      hour
  %l      hour, 12 hour clock
  %m      month number, starting with 1
  %M      minute, leading 0
  %n      NEWLINE
  %o      ornate day of month -- "1st", "2nd", "25th", etc.
  %p      AM or PM 
  %q      Quarter number, starting with 1
  %r      time format: 09:05:57 PM
  %R      time format: 21:05
  %s      seconds since the Epoch, UCT
  %S      seconds, leading 0
  %t      TAB
  %T      time format: 21:05:57
  %U      week number, Sunday as first day of week
  %w      day of the week, numerically, Sunday == 0
  %W      week number, Monday as first day of week
  %x      date format: 11/19/94
  %X      time format: 21:05:57
  %y      year (2 digits)
  %Y      year (4 digits)
  %Z      timezone in ascii. eg: PST
  %z      timezone in format -/+0000

date_into_object( $date_string )

Takes apart $date_string and returns a Class::Date object. You can call a number of methods on this object to get individual pieces of a date. (See the docs for Class::Date for a complete list.)

Note that you can pass 'now' or 'today' as $date_string and get the current time.


  [% mydate = '2000-5-1 5:45 PM' %]
  [% dt = OI.date_into_object( mydate ) %]
  Date: [% mydate %]
  Year: [% dt.year %]
  Month Num/Name: [% dt.month %] / [% dt.monthname %]
  Day/Name/of Year:  [% %] / [% dt.wdayname %] / [% dt.day_of_year %]
  Hour: [% dt.hour %]
  Minute: [% dt.minute %]


  Date: 2000-5-1 5:45 PM
  Year: 2000
  Month Num/Name: 5 / May
  Day/Name/of Year:  1 / Monday / 121
  Hour: 5
  Minute: 45

limit_string( $string, $length )

Returns a string of max length $length. If the function removes information from the string, it appends '...' to the string. Note that we currently do not try to be nice with word endings.


 [% string = 'This is a really long news title and we have strict space constraints' %]
 [% OI.limit_string( string, 25 ) %]


 This is a really long new...

javascript_quote( $string )

Performs necessary quoting to use $string as Javascript code. Currently this only involves escaping the "'" character, but it can easily expand as necessary.


 [% book_title = "it's nothing" %]
 var newArray = new Array( '[% OI.javascript_quote( book_title ) %]' );


 var newArray = new Array( 'it\'s nothing' );

We could probably use a filter for this.

regex_chunk( $string, $match )

Tries to match $match in $string and returns the matching text.


  Matching text: ([% OI.regex_chunk( 'This is the text', 'This (.*)' ) %])


  Matching text: (is the text)

limit_sentences( $string, $num_sentences )

Limits $string to $num_sentences sentences. If the resulting text is different -- if the function actually removes one or more sentences -- we append '...' to the resulting text.


  [% sentence_text = 'This is the first. This is the second. This is the third. This is the fourth.' %]
  Sentences: [% OI.limit_sentences( sentence_text, 2 ) %]


  Sentences: This is the first. This is the second. ...

percent_format( $number[, $places ] )

Formats $number as a percentage to $places. If not specified $places defaults to '2'.


 [% grade = 44 / 66 %]
 Grade: [% OI.percent_format( grade, 2 ) %]


 Grade: 66.67%

money_format( $number[, $places ] )

Displays $number as US dollars to $places. If not specified, $places defaults to 2.


  [% monthly_salary = 3000 %]
  [% yearly_salary = monthly_salary * 12 %]
  Your yearly salary: [% OI.money_format( yearly_salary, 0 ) %]


  Your yearly salary: $36000

byte_format( $number )

Displays $number as a number of bytes. If the number is less than 1024 it displays directly, between 1024 and 1024**2 as KB, between 1024**2 and 1024**3 as MB and greater than that as GB.


 The file sizes are:
   [% OI.byte_format( 989 ) %]
   [% OI.byte_format( 2589 ) %]
   [% OI.byte_format( 9019 ) %]
   [% OI.byte_format( 2920451 ) %]
   [% OI.byte_format( 920294857 ) %]
   [% OI.byte_format( 3211920294857 ) %]


 The file sizes are:
   989 bytes
   2.5 KB
   8.8 KB
   2.8 MB
   877.7 MB
   2991.3 GB

uc_first( $text )

Simply upper-case the first letter of the text passed in. Note that we do not do every word, just the first.


  [% first_name = 'yahoo' %]
  Hi there, [% OI.uc_first( first_name ) %]


  Hi there, Yahoo

html_encode( $text )

Encodes $text so that it can be displayed in a TEXTAREA or in other widgets.


 [% news_item = "<p>This is the first paragraph</p>" %]
 <textarea name="news_item" cols="50" rows="4"
           wrap="virtual">[% OI.html_encode( news_item ) %]</textarea>


 <textarea name="news_item" cols="50" rows="4"
           wrap="virtual">&lt;p&gt;This is the first paragraph&lt;/p&gt;</textarea>

html_decode( $text )

Decodes $text with HTML entities to be displayed normally.


 [% news_item = '&lt;p&gt;This is the first paragraph&lt;/p&gt;' %]
 [% OI.html_decode( news_item ) %]


 <p>This is the first paragraph</p>

make_url( \%params )

Creates a URL given a number of parameters, taking care to perform any necessary transformations.


All parameters except those listed below are assumed to be used as GET keys and values and will be appended to the URL appropriately.


 [% user_show_url = OI.make_url( base = '/User/show/',
                                 user_id = user.user_id ) %]
 <a href="[% user_show_url %]">blah</a>

displays (when under the normal location of '/'):

 <a href="/User/show/?user_id=5">blah</a>

displays (when under a different location '/oi'):

 <a href="/oi/User/show/?user_id=5">blah</a>

page_title( $title )

Tell OpenInteract to use $title for the page title.


 [% OI.page_title( 'My Favorite Quotes from Douglas Adams' ) %]

use_main_template( $template_name )

Tell OpenInteract to use a particular main template. The $template_name should be in 'package::name' format.


  [% OI.use_main_template( 'mypkg::main' ) -%]

theme_fetch( $new_theme_spec, \%params )

Retrieves the properties for theme $new_theme_spec, which can be an ID (normal) or a name listed in the 'default_objects' of your server configuration. If the latter we'll use the ID associated with that name.

If the key set_for_request is set to 'yes' in \%params then this new theme will be used for the remainder of the request. This includes the main template along with all graphical elements.

Returns: hashref with all properties of the given theme.


 [% new_theme = OI.theme_fetch( 5 ) %]
 Background color of page from other theme: [% new_theme.bgcolor %]
 [% new_theme = OI.theme_fetch( 5, set_for_request = 'yes' ) %]
 Background color of page from other theme: [% new_theme.bgcolor %]
 Hey, the new theme is now set for the rest of the request!



A hashref with all the properties of The current theme. You will probably use this a lot.


 [% theme = OI.theme_properties %]
 <tr bgcolor="[% theme.head_bgcolor %]">

The exact properties in the theme depend on the theme. See the 'base_theme' package for more information.


Alias for 'theme_properties'


The user object representing the user who is currently logged in.


 [% login = OI.login %]
 <p>Hi [% login.full_name %]! Anything new?</p>


An arrayref of groups the currently logged-in user belongs to.


 [% login_group = OI.login_group %]
 <p>You are a member of groups:
 [% FOREACH group = login_group %]
   [% th.bullet %] [% %]<br>
 [% END %]


True/false determining whether the user is logged in or not.


 [% IF OI.logged_in %]
   <p>You are very special, logged-in user!</p>
 [% END %]


True/false depending on whether the user is an administrator. The definition of 'is an administrator' depends on the authentication class being used -- by default it means that the user is the superuser or a member of the 'site admin' group. But you can modify this based on your needs, and make the result available to all templates with this property.


 [% IF OI.is_admin %]
   <p>You are an administrator -- you have the power! It feels great,
 [% END %]


Contains all information currently held in the session. Note that other handlers may during the request process have modified the session. Therefore, what is in this variable is not guaranteed to be already saved in the database. However, as the request progresses OpenInteract will sync up any changes to the session database.

Note that this information is read-only. You will not get an error if you try to set or change a value from the template, but the information will persist only for that template.


 [% session = OI.session %]
 <p>Number of items in your shopping cart:
    [% session.num_shopping_cart_items %]</p>


What the 'return url' is currently set to. The return url is what we come back to if we have to do something like logout.

<a href="[% OI.return_url %]">Logout and return to this page</a>


A hashref representing a container with all error messages as generated by error handlers. The error handler and the template need to coordinate on a naming scheme so you know where to find your messages.

Note that future work may restrict this to the errors for your template only.


 [% error_messages = OI.error_hold %]
 <p>User [% error_messages.loginbox.login_name %] does not exist in
 the system.</p>


A hashref with keys of 'none', 'read', and 'write' which gives you the value used by the system to represent the security levels.


 [% IF obj.tmp_security_level < OI.security_level.write %]
  ... do stuff ...
 [% END %]


A hashref with the keys of 'user', 'group' and 'world' which gives you the value used by the system to represent the security scopes. This will rarely be used but exists for completeness with security_level.

 [% security_scope = OI.security_scope %]
 [% FOREACH scope = security_scope.keys %]
   OI defines [% scope %] as [% security_scope.$scope %]
 [% END %]


Returns the server configuration object (or hashref) -- whatever is returned by calling in normal code:


 The ID of the site admin group is: 
  [% OI.server_config.default_objects.site_admin_group %]


The following were removed from the old module OpenInteract::Template::Toolkit:


None known.


Custom plugins

Make it easy for websites to create their own plugins that can be accessed through the 'OI.' plugin. For instance, a package owner could define a set of additional behaviors to go along with a package. In a file distributed with the package, the plugins could be listed:


And stored within the server-wide configuration object. Then when we call load() in this plugin, we could do something similar to the _populate method in Slash::Display::Plugin::Plugin where we peek into the @EXPORT_OK array and copy the code refs into a hash which we can then check via AUTOLOAD.

One problem with that is name collision -- two packages might both define a 'do_stuff' action, and in this case the last one would win. No good.

Maybe we prepend the package name to the action? Also no good -- the whole idea is to make the template environment transparent....



Template::Plugin::URL for borrowed code

Slashcode ( for inspiration


Copyright (c) 2001-2002, inc.. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Chris Winters <>

syntax highlighting: