Code Templates for Zend Studio

Happy Easter!

Last time I talked about Test First. In the meantime, the SimpleTest framework has been integrated into ATK. It's in the current nightly builds. When the next version of ATK is released, I'll write a howto for it.

Today I want to cover a different topic though. I use the Zend Studio IDE for all my PHP coding. At ibuildings, it's the standard development environment. I can recommend it to anyone using ATK. (the 'personal edition' is free, but the professional edition isn't expensive either).

The code completion feature alone is excellent when using ATK. What was the name of that attribute? What were the parameters? With Zend Studio you don't need to remember. It'll tell you on the fly.

Ok, before this becomes a Zend commercial ;-) , I want to lift out one particular feature. Code Templates. It's kind of hidden in Zend Studio, but it's extremely powerful. It eases coding a lot by providing templates for much used constructs. I've discovered that for creating ATK nodes, it saves a tremendous amount of typing.

There are several templates already provided in the IDE. I noticed that none of those starts with the letter 'a'. So I've made all my ATK templates start with an 'a', followed by just a few letters. Now, whenever I type 'anode' and press enter, a complete node code frame is put in place.

(If you use a different IDE that supports templates, you may be able to use the examples too, but the syntax might be different.)

Installing the templates

If you want to create the templates by hand, go to Tools->Preferences->Templates. Click the 'add' button. Enter the name, a description and choose 'PHP' for the context. Then copy the code into the 'Template code' field.
You can also download my template file here (right-click -> 'save as'). Replace (at your own risk ofcourse) the original in C:\Documents and Settings\user\ZDE\config_4.0\templates or whereever your Zend settings are stored. If you already have custom templates, edit your PHP.xml file and add the content from this file to it, to add the ATK templates.

anode
This is my template for generating a node body.

 
/**
 * The node class for ${name}.
 */
class ${name} extends atkNode
{
  function ${name}()
  {
    $this->atkNode("${name}");
    $this->add(new atkAttribute("id", AF_AUTOKEY));
 
    ${END}
 
    $this->setTable("${name}");
  }
 
  /**
   * Set up default values for this node
   * @return array Associative array where the key is
   *               the attribute name, and value is the
   *               default value.
   */
  function initial_values()
  {
    return array();
  }
 
  /**
   * Return record descriptor (for use in relations etc.)
   * @return String A template containing fieldnames between brackets.
   */
  function descriptor_def()
  {
    // @todo: replace this with proper descriptor fields.
    return "[id]";
  }
}

Whenever I type 'anode' in a file and press enter, this code is put in, and it allows me to give the node a name. The name is then filled in automatically everywhere it's needed (constructor, setTable etc.). Since most of my nodes have a primary key called 'id', I put this in the template. I also added the often used methods initial_values() and descriptor_def(). I think every node should have a descriptor_def, as you never know which other nodes might make relations with this node, and in that case, you want this node to look good.

Notice how I also add docs. It's easy to put docs in the template that adhere to your personal or company coding guidelines. The ${END} marker makes sure that the caret is at the position in the code where 99% of the time, you will start adding attributes to your node.

aattr

I use this code to add attributes. In the future I will add more of these for specific much-used attributes such as atkTextAttribute, atkDateAttribute etc. For now, it only does the basis atkAttribute:

 
$this->add(new atkAttribute("${name}", ${flags}));
${END}

amto

I use atkManyToOneRelations a lot, so I've created this template to insert one:

 
$this->add(new atkManyToOneRelation("${name}", "${module.node}", ${flags}));
${END}

I haven't mentioned yet that the ${..} parts are 'fill in the blanks' variables. When you add the template to your code by entering the template name and pressing enter, the editor will let you fill in the correct values for these variables. Pressing enter again jumps to the next variable, until you completed the template. Fun thing is that while doing so, code completion and suggestion still works, so in the flags part, pressing 'AF_' is enough to popup the list of possible flags.

aotm

Another relation that's often used is the atkOneToManyRelation. This templates will insert it in your node:

$this->add(new atkOneToManyRelation("${name}", "${module.node}",
"${CURRENT_CLASS}_id", ${flags}));

I've used a nice feature of the templates here. As you know, atkOneToManyRelations need to be told what the foreign key in the destination class is. In my case, in 99% of the cases, this is the name of the node with '_id' appended to it. By using ${CURRENT_CLASS}, the template will automatically fill in the classname here. Since that is the same as the node itself, this is a very good guess at what the foreign key will be. For example, if in the node 'project' I create a one-to-many relation with 'teammembers', then this template will automatically fill in 'project_id' for the foreign key. Ofcourse, after the template has been put in your code, it's just normal code, so for the 1% where this is the wrong key, you can just change it.

aaction

My final template for now is a template that I use for overriding actions. Often, I'm overriding action_admin, action_edit etc, to add filters, make some dynamic changes to attributes etc. This templates puts the action override in place and makes sure I don't forget the original action handler at the end.

Here's the template code for the 'aaction' template:

 
/**
 * Implements/overrides the ${name} action.
 */
function action_${name}(&$handler)
{
  ${END}
 
  // Call original handler
  return $handler->action_${name}();
}
 

I will add more templates in the future. Suggestions are welcome.

Tags: , , , ,

3 Responses to “Code Templates for Zend Studio”

  1. Yury Says:

    And other feature, which force to use ZEND – its perfect PHP debugger!

  2. Jamie Tomlinson Says:

    You mention the Personal edition being free? I have gone to the Zend Studio website and they only mention Professional or Standard? Does anyone know how to get hold of the personal edition or have they rationalised their products?

  3. Ivo Says:

    Hmm, you’re right, I can’t find it either. I’m sure there used to be a free ‘personal edition’. Let me ask my account manager at Zend and get back to you on that.

Leave a Reply