[jifty-devel] Difference Between alias and import_templates in Template::Declare

Shawn M Moore sartak at bestpractical.com
Tue Oct 13 14:36:46 EDT 2009


Hi David,

On Fri, Oct 09, 2009 at 07:20:30PM -0700, David E. Wheeler wrote:
> Template class Foo defines template "hello".
> 
> Template class Foo::Bar inherits from Foo.
> 
> Template class Wifty::UI aliases Foo::Bar under /bar.
> 
> So what this means is that Wifty::UI has the template /bar/hello.
> 
> Now, everything is nearly the same as it was before: the code in the  
> "hello" template can access package variables (if any were set) via ` 
> $self->get_package_variable`.
> 
> The *only* difference is that $self is now "Foo", whereas before it  
> would have been "Foo::Bar". This has no effect on the package  
> variables that can be fetched.
> 
> The only way in which I can imagine that this would bit a user is if  
> they were expecting Foo::Bar. Maybe Foo::Bar had another class method  
> that they would want access to. That makes no sense to me, though,  
> because the "hello" template is defined in the Foo class, and  
> therefore should have no knowledge of its subclasses. Maybe Foo::Bar  
> overrode a method defined in Foo. That might lead to something  
> unexpected. But I have a hard time believing that anyone would have  
> done this, not least because these are classes with class attributes,  
> not objects. So there isn't really much in the way of state.

I just want to point out that this is an important use case for us. We
do use the object-oriented features of Template::Declare and it is
important that they continue to work.

The most prevalent use of TD's OO is in Jifty's CRUD builder. Basically,
the CRUD builder is set up like:

sub record_class { ... }

sub display_columns { $self->record_class->... }
sub edit_columns    { $self->record_class->... }

template 'view_item' => sub { ... }
template 'edit_item' => sub { ... }

I don't think we instantiate an object. We just make use of the
indirection of OO to let users subclass the CRUD builder to customize
it. They can, for example, change which columns are editable by
overriding the edit_columns method.

> The *only* difference is that $self is now "Foo", whereas before it  
> would have been "Foo::Bar". This has no effect on the package  
> variables that can be fetched.

We also typically use "alias" to nail particular CRUD classes into the
template tree, so this is kind of a showstopper. :)

As I understand it, you have since fixed this, but I felt you deserved a
good explanation as to why we pass in the particular class we do.

Shawn


More information about the jifty-devel mailing list