<commit_before>How to add a custom helper
==========================

Overview
--------

Symfony offers various helpers to accelerate template development (see more in the [View chapter](view.txt)). In addition, in every new project can arise the need for custom helpers used either in a specific module, or throughout the whole application. These custom helpers can be used as the standard ones, via a simple function call in the templates.

Naming conventions
------------------

To add custom helpers available to your templates, simply create a PHP file ending with `Helper.php` and place it in a subdirectory of the PHP `include_path` named `helper`. The directory where the helper is placed will determine its availability. For instance:

    Helper placed in                  Is available for
    ----------------                  ----------------
    lib/helper                        all applicationsof the project
    myapp/lib/helper                  all the modules of the 'myapp' application
    myapp/modules/mymodule/lib/helper all the templates of the 'mymodule' module

To use a helper in a template, you must first declare the helper in the action:

    $this->userHelper('Name');

Example
-------

Let's imagine that your application uses the wiki syntax to format the data entered by users. The wiki to HTML conversion may occur quite often, so you will factorize the code in a 'wiki_to_html()' helper function:

Here is the `WikiHelper.php` file stored in the `myapp/lib/helper/` directory:

    <?php
    
    public function wiki_to_html($text)
    {
      require_once 'Wiki.class.php';
      $wiki = new Wiki();
      return $wiki->transform($text);
    }
    
    ?>

Here is an example module using the helper:

    class articleActions extends sfActions
    {
      public function preExecute()
      {
        $this->useHelper('Wiki');
      }
    
      ...
    }

And here is the call to the helper in a template of this module:

    <p><?php echo wiki_to_html('Text with a WikiLink.') ?></p>
    
Accessing context objects
-------------------------

In a helper, shortcuts like `$request` or `$user` don't work, neither do the action-style calls (`$this->getRequest()`, `$this->getUser()`, etc.) since the function is not in an object context. So, in order to access the context objects, you have to use the `sfContext` singleton (`sfContext::getInstance()`). For instance, to get the request parameters, write:

    $id = sfContext::getInstance()->getRequest()->getParameter('id');<commit_msg>fixed an outdated helper declaration in the doc<commit_after>How to add a custom helper
==========================

Overview
--------

Symfony offers various helpers to accelerate template development (see more in the [View chapter](view.txt)). In addition, in every new project can arise the need for custom helpers used either in a specific module, or throughout the whole application. These custom helpers can be used as the standard ones, via a simple function call in the templates.

Naming conventions
------------------

To add custom helpers available to your templates, simply create a PHP file ending with `Helper.php` and place it in a subdirectory of the PHP `include_path` named `helper`. The directory where the helper is placed will determine its availability. For instance:

| Helper placed in                    | Is available for
| ----------------------------------- | ----------------
| `lib/helper`                        | all applications of the project
| `myapp/lib/helper`                  | all the modules of the 'myapp' application
| `myapp/modules/mymodule/lib/helper` | all the templates of the 'mymodule' module

To use a custom helper in a template, you must first declare it on top of the template, as for other helpers:

    <?php use_helper('Name') ?>

Example
-------

Let's imagine that your application uses the wiki syntax to format the data entered by users. The wiki to HTML conversion may occur quite often, so you will factorize the code in a 'wiki_to_html()' helper function:

Here is the `WikiHelper.php` file stored in the `myapp/lib/helper/` directory:

    <?php
    
    public function wiki_to_html($text)
    {
      require_once 'Wiki.class.php';
      $wiki = new Wiki();
      return $wiki->transform($text);
    }
    
    ?>

Here is an example template using the helper:

    <?php use_helper('Wiki') ?>
    ...
    <p><?php echo wiki_to_html('Text with a WikiLink.') ?></p>
    
Accessing context objects
-------------------------

In a helper, shortcuts like `$request` or `$user` don't work, neither do the action-style calls (`$this->getRequest()`, `$this->getUser()`, etc.) since the function is not in an object context. So, in order to access the context objects, you have to use the `sfContext` singleton (`sfContext::getInstance()`). For instance, to get the request parameters, write:

    $id = sfContext::getInstance()->getRequest()->getParameter('id');
{'fraction_non_alphanumeric': 0.12833468067906226, 'fraction_numerical': 0.0, 'mean_word_length': 2.8633879781420766, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '953626', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1225, 'n_words': 596}