views:

63

answers:

1

So as I am architecting my project, I'm thinking I must be doing something wrong. Some pieces of template code are reusable that makes me want to extract the code out of the template, but I can't find a good way. For example, some buttons are the same design all throughout the website. What's the best way to extract it out of the page? This is where I considered the use of simple, inclusion tags, or include files.

Now this is fine, except I'm also very concerned with optimizing for speed. After doing tests, I've found that using simple tags is 50% slower than just normal inline templating and inclusion tags are more than 350% slower. This is a concern because some pieces of code that are duplicated are in places that are for looped down the list. So for instance, when showing a list of items, I have a piece of code for up/down voting that is duplicated across the site. If I use something like inclusion tag, that would add a lot of heavy lifting for the site. Is there a better way to do all of this?

If I'm building for speed, should I essentially compromise DRY to make things faster?

+1  A: 

Use whatever stops you repeating yourself. I'm a fan of inclusion tags for things like buttons.

If you're building for speed, use Django's cache framework, to avoid doing the same work over and over again, particularly looking at template fragment caching.

Dominic Rodger
Exactly. This is premature optizimation. It's unlikely the template system will really be the bottleneck; more importantly, you don't know yet if it is. So you'd be building less maintainable templates in order to optimize something you don't even know needs to be optimized. Also, Django 1.2 adds caching of loaded templates, which might make this performance hit mostly go away.
Carl Meyer
thanks guys, I think you're absolutely right. Carl, I think your comment is very telling. I think I have been obsessing over the wrong things. Thanks for the heads up :)