(You are Anonymous)

HTML::Template::Plugin::Dot - Magic Dot Notation for HTML::Template!

Rhesa Rozendaal and Mark Stosberg recently added the "magic dot" notation to HTML::Template, giving HTML::Template the ability to process more complex data structures within templates, such as objects and object methods.

How does it work? Simply pass your data structure, etc. (in this example, a Class::DBI object) as a parameter to your template as such:

sub view
{
    my $self = shift;
    my $username = $self->query->param("user");
    my $user     = My::Users->retrieve($username);

    # Load the edit page
    my $tmpl_view = $self->load_tmpl( "view_user.tmpl" );

    # The magic happens here!  Pass our Class::DBI object
    # to the template and display it
    $tmpl_view->param( user => $user );

    return $tmpl_view->output;
}

The template to display this:

<h2>View User</h2>

<tmpl_if user.login_name>
<p>
    <table>
        <tr>
            <td><b>User Name:</b> </td>
            <td><tmpl_var user.login_name></td>
        </tr>
    </table>
</p>
<tmpl_else>
<p>The specified user does not exist.</p>
</tmpl_if>

Notice that the template is calling the login_name method on the <tmpl_var> user (which is really the object $user). This value is then displayed on the resulting page.

While the "magic dot" will likely not be supported in the base of CGI::Application, you can use it easily through a couple of different plugins:

See Also