Monthly Archive for August, 2006

Custom Plugin Functions

I wrote a Smarty plugin function. It’s cool.

What we have is a bunch of PHP pages which all use the same Smarty templates to display the top of the page. I wanted to add a bunch of PHP code to the pages. Thing is, the PHP pages are all encoded with ionCube, so you can’t see the code, you can’t modify the code, you’re out of luck. The only thing you can modify is the Smarty template.

So I read some documentation and found out I could define my own functions as a Smarty plugin. How cool is that? I wrote my function, as PHP, and put it in the plugin directory, and then I can call it from the Smarty templates that get loaded by every PHP page. Frickin slick.

More Smarty Coolness

I like Smarty Templates more and more every day. At first I was frustrated that JavaScript functions, which begin and end with curly brackets {}, were causing the template to choke. But then I read the obvious, that you should be putting your JavaScript in a separate file. It’s nice how they (almost) force you to modularize everything. I also really like {section} and being able to loop through things. I took a page that displayed a variety of counter styles that was 277 lines and reduced it to 24. Smarty is just cool.

Learning Smarty

The last few days I’ve been teaching myself Smarty Templates for PHP. They are so. Cool. Smarty allows you to really separate your business logic from your presentation logic. Meaning, your PHP code does all the data molestation, and the Smarty template just presents it to the user. This is A Good Thing because it means you can change your PHP without messing up the front end, you can change your backend without having to change the user interface. And you can change the style sheets, the images, the layout, all that without having to change your PHP. You keep the two separate so that future modifications are cleaner and easier. Freakin slick, says I.

Calculating Values for Paging

I have to figure this out every time I do anything with paging, so I’m going to put it here. When I’m doing things with pages, you have to figure out things like, what’s the index of the first entry I’m going to display on this page, and how many pages do I have total?

To calculate the total number of pages, this is what I like to do. Given the number of entries (e) and the number of entries to display per page (d):

$n = ceil(e / d);
return max(1, $n);

To calculate the zero-based index of the entry to begin displaying on page (p) I need to know the number of entries to display per page (d):

$startAt = (p - 1) * d;

So. If you have 20 entries and you display 3 entries per page, there are seven pages. Page 4 will display entries beginning at 9.