Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Augmenting Existing Helper Groups

In previous snippets, I gave examples of new helpers. Some of these helpers could fit into already existing helper groups in the symfony core. I then suggested adding a helper group, like "NumberPlus" to augment the "Number" helper group.

There is a nicer way to do this. To augment the Number helper group without needing to redefine existing functions, add a file in <proj>/apps/<app>/lib/helper/NumberHelper.php and place the following in it:

The Code

<?php
 
include_once('symfony/helper/NumberHelper.php');
 
// custom Number helpers go here
function my_number_helper()
{
  // ...
}
 
// ...

Since symfony's bootstrapping process adds the appropriate paths to the include path of php, this works nicely. Of course, this assumes that your installation of symfony isn't customized too much, so you may need to tweak the path a little bit.

Now, when you need access to predefined symfony Number helpers, or your custom helpers, just use:

Example

<!-- someTemplateSuccess.php -->
<?php use_helper('Number') ?>
 
<?php echo format_number(5) ?>
 
<?php echo my_number_helper() ?>

NOTE: I haven't experimented with this outside of my setup, but since my installation of symfony isn't too unusual, I'm assuming it works nicely. If it doesn't work for you, please post the details so I can modify this snippet if necessary.

by Stephen Riesenberg on 2007-04-18, tagged helper 
You need to create an account or log in to post a comment or rate this snippet.