Search code examples
perlcatalystdbix-classtemplate-toolkit

Moving logic from Template Toolkit to Catalyst


I think that I am using too much conditionals and calculations in the TT templates.

I am displaying a result set of items from DBIc. For each item I need to calculate things using the retrieved values, and the template doesn't seems to be the right place.

But in Catalyst it is a thick object that comes from DBIc.

So how can I move logic to the model? Must I run a whole loop for all items and change the object somehow?

Regards: Migue,


Solution

  • First, you're on the right track by wanting to properly separate concerns. You'll thank yourself if you're the maintainer 6-12 months down the road.

    IMHO, your Catalyst controllers should be as thin as possible with the business logic in the various models. This makes it easier to test because you don't have the overhead of Catalyst to deal with. I've been thinking about model separation quite a bit myself. There are two schools of thought I've come across:

    1) Make your DBIx::Class Result classes have the business logic. This approach is convenient and simple.

    2) Make a standalone Model which is instantiated by the Controller, and which has a DBIx::Class schema object. The model would use the DBIC schema to query the database, and then use the resulting data in its own business logic methods. This approach might be better if you have a lot of business logic since you separate DB access from business logic.

    Personally, I've historically used approach #1 but I'm leaning towards #2 for larger apps.