Category Archives: Developer Toolbox

Vim tips and tricks: HTML table re-ordering

Suppose you have an HTML table on a static website, not dynamically created. It shows a grid of thumbnail photos in table cells. The code was something like the following:

<table>
 <tr>
 <td><a href="photo/photo01.html"><img src="photo/photo01.jpg" alt="" /></a></td>
 <td><a href="photo/photo02.html"><img src="photo/photo02.jpg" alt="" /></a></td>
 <td><a href="photo/photo03.html"><img src="photo/photo03.jpg" alt="" /></a></td>
 <td><a href="photo/photo04.html"><img src="photo/photo04.jpg" alt="" /></a></td>
 <td><a href="photo/photo05.html"><img src="photo/photo05.jpg" alt="" /></a></td>
 </tr>
 <tr>
 <td><a href="photo/photo06.html"><img src="photo/photo06.jpg" alt="" /></a></td>
 <td><a href="photo/photo07.html"><img src="photo/photo07.jpg" alt="" /></a></td>
 <td><a href="photo/photo08.html"><img src="photo/photo08.jpg" alt="" /></a></td>
 <td><a href="photo/photo09.html"><img src="photo/photo09.jpg" alt="" /></a></td>
 <td><a href="photo/photo10.html"><img src="photo/photo10.jpg" alt="" /></a></td>
 </tr>
 <!-- etc... -->
</table>

Yes, ouch!

There are hundreds of photos, spread over several pages in several HTML files, and you want to put them all on one page. You also want to change the number of columns of the thumbnail grid. How are you going to do this in the most efficient way possible, without turning the code into a dynamically generated code? Read on to learn more, using grep and vim!

Continue reading Vim tips and tricks: HTML table re-ordering

Sending mails from a Zend application

If you are making an application that needs to send out e-mails to for example hundreds of your users, then you don’t really want to actually send them e-mails when testing your application in a development environment. They would totally not appreciate getting multiples of your test e-mails, probably littered with loads of debugging code.

However, in Zend Framework you can have a different configuration for your development environment. One of these configuration items is the mail transport system. If you tell Zend to use the following for your development environment:

resources.mail.transport.type = Zend_Mail_Transport_File
resources.mail.transport.path = APPLICATION_PATH "/../mails"

then the Zend Framework will put all e-mails sent using the Zend_Mail class, in this mails directory in your project’s root directory. This way, you can safely test all the out-going e-mails that are sent by your application, and check them out in the mails directory.

There is still one more thing to do. If you do the above, you will notice that Zend creates *.tmp files that are in a certain ugly MIME encoding format (7bit, quoted-printable or base64). What is more, you will have a hard time checking the HTML layout of your HTML e-mails, since you’ll only see HTML code in the *.tmp files for your HTML e-mails, which are also encoded in the aforementioned ugly mail encoding.

Add mutt to your toolkit, configure your Zend mail transport to put the mails in the new directory of the mutt tree, and you’ll be able to access your e-mails using mutt!

First the Zend configuration:

resources.mail.transport.type = Zend_Mail_Transport_File
resources.mail.transport.path = APPLICATION_PATH "/../mails/new"

Then install mutt and prepare the directories:

sudo apt-get install mutt
mkdir -p mails/tmp mails/cur mails/new
chgrp www-data mails/new
chmod g+w mails/new

Start mutt, telling it to use the mails directory:

mutt -f mails

And there you go, you’ll be able to see incoming mails, sent from your Zend application. No need to fiddle with the addTo() calls that could be everywhere.

To check the HTML version in your mutt e-mail client, open the e-mail, hit v to see the attachments, navigate to the text/html part, hit Enter, and mutt will open it in your default web browser.

Zend Framework 1 Extension Library and Composer

Maybe I am a bit late to get on the Composer bandwagon, but a colleague was adamant about it and convinced me to use Composer in our projects. The benefit of using Composer with your PHP applications is that it will download any library dependencies for you, and take care of the autoloading of classes. So I enabled Composer in my Zend Framework 1 Extension library, with a Zend Framework 1.* dependency. Read on how to get the libraries using Composer!

Continue reading Zend Framework 1 Extension Library and Composer

tmux – an awesome little tool

If you do a lot of programming from the Linux console, you’ll certainly appreciate the power of tmux. It is a terminal multiplexer, which means that it can create multiple virtual consoles within a single terminal shell. Since these virtual consoles are not bound to a terminal, they will keep running in the background, even if you close the terminal you started it in. This means that you can detach these consoles, and re-attach them in another terminal session.

For example, imagine you are working on some project on a development server. You log into that server over SSH from your office, create a tmux session, and do your coding in vim in there. Then you detach the session, and log out from the server to go home. Back at home, you log into the development server again, and re-attach the tmux session, and you see the vim session back where you left it. Not that I suggest you to work from home, but it is a good example of how you can continue your work or session from anywhere.

Tmux in action. Two vim sessions and a MongoDB console.
Tmux in action. Two vim sessions and a MongoDB console.

Another great thing about tmux is that you can create multiple sessions within the same terminal window, and move between them, something like alt-tabbing between terminal windows, except it is much faster and with less visual distraction. An added bonus is that tmux is able to subdivide the visible portion of your terminal window into parts, so you can have multiple virtual consoles, all visible within one window. Very useful if you have lots of screen estate at your command, so that you can code in vim in one, access a MySQL console in another, and doing a sass --watch in yet another, for example. All the information you need, everything visible in the same window, for optimal productivity.

Continue reading tmux – an awesome little tool

MySQL pager configuration

Have you ever typed in a SQL query that returns thousands and thousands of results, and you had to Ctrl-C to stop it, which would then also terminate your MySQL console process? Annoying, isn’t it!

Well, there is actually a pager configuration for MySQL. My favorite pager is less, ’cause less is more, right? Paradoxically, it has more features than more, for in more you wouldn’t be able to scroll up, for example.

To make MySQL use less as the pager, you should add the following line to your my.cnf file, in the [client] block:

[client]
pager = "less -X -F"

Here is what the arguments do:

  • -X: prevent less from clearing the screen when exiting. This is useful when you want to have the results on screen after quitting less, to be able to compose your next SQL command
  • -F: tells less to quit immediately if the buffer fits on one screen. Without this, you’d have to quit less first before you can type the next SQL command, even though it fitted perfectly on one screen.