Code snippets for symfony 1.x


Automatic Javascript Include

If you're like me and prefer to code your own Unobtusive Javascript, you'll probably find this snippet handy. It will search through your project's /web/js folder for any .js files that match the current Symfony action.

For example, the snippet/new action would look for /web/js/snippet/snippet.js and /web/js/snippet/new.js, and add them to the response if either exist.

You can also define an optional subfolder within /web/js where you want the filter to look (i.e. "backend").

The Code

 * Looks for Javascript files based on current action/module and adds them to
 * the current response object.
 * Add this filter to the end of your filter chain in filters.yml, after the 
 * execution filter:
 * <code>
 *     rendering: ~
 *     web_debug: ~
 *     security:  ~
 *     # generally, you will want to insert your own filters here
 *     cache:     ~
 *     common:    ~
 *     flash:     ~
 *     execution: ~
 *     auto_javascript_include:
 *       class: myAutoJavascriptIncludeFilter
 * </code>
 * @package     Automatic Javascript Include (AJI)
 * @subpackage  filter
 * @author      Kris Wallsmith <kris [dot] wallsmith [at] gmail [dot] com>
 * @version     SVN: $Id$
 * @copyright   Have at it ...
class myAutoJavascriptIncludeFilter extends sfFilter
     * Include external Javascript files based on last action called.
     * This kicks in on the way back down the filter chain, so we're sure to
     * catch the last action. Looks through the web/js folder for files that
     * match the naming syntax and adds them to the response. 
     * You can specify a subfolder of the web/js folder for the filter to 
     * search in app.yml.
     * @author  Kris Wallsmith <kris [dot] wallsmith [at] gmail [dot] com>
     * @see     sfConfig::get('app_aji_subfolder')
     * @param   sfFilterChain $filterChain
    public function execute($filterChain)
        $sf_context  = sfContext::getInstance();
        $sf_response = $sf_context->getResponse();
        $sf_web_dir  = sfConfig::get('sf_web_dir');
        $module = $sf_context->getModuleName();
        $action = $sf_context->getActionName();
        $sub_folder = sfConfig::get('app_aji_subfolder');
        if($sub_folder && $sub_folder{0} != '/') $sub_folder = '/' . $sub_folder;
        $fmt = '/js%s/%s/%s.js';
        $mod_mod_js = sprintf($fmt, $sub_folder, $module, $module);
        $mod_act_js = sprintf($fmt, $sub_folder, $module, $action);
        if(file_exists($sf_web_dir . $mod_mod_js)) $sf_response->addJavascript($mod_mod_js);
        if(file_exists($sf_web_dir . $mod_act_js)) $sf_response->addJavascript($mod_act_js);
by Kris Wallsmith on 2007-04-24, tagged filter  javascript 

Comments on this snippet

gravatar icon
#1 Stephen Riesenberg on 2007-04-25 at 06:37

I actually wrote a filter to do the same type of thing with css. Maybe I'll post that as a complement to this one. Nice stuff.

gravatar icon
#2 markus staab on 2007-04-25 at 02:38

you should follow the symfony code conventions and use camel-case instead of the underscores..

gravatar icon
#3 Kris Wallsmith on 2007-04-25 at 06:20

kills: Are you referring to conventions apparent in the Symfony code, or are these actually documented somewhere?

gravatar icon
#4 Karl Katzke on 2007-04-25 at 06:20

Markus - You mean symfony CamelCase conventions like $sf_request and use_helper()?

gravatar icon
#5 Stephen Riesenberg on 2007-04-25 at 09:42

C'mon guys. The code is fine. Local scalar values like strings (eg. $sub_folder) don't matter at all. Besides, variables named after database columns (my_table_id) and symfony config vars (sf_web_dir) always use underscores, not to mention helpers and templating variables, like karlkatzke mentioned.

(BTW: I don't really find it appropriate to rag on picky issues like that, especially when they make no difference.)

gravatar icon
#6 Jason Ibele on 2007-04-30 at 05:00

Frankly, I think his code is fine. But just to enlighten you all, there is a symfony coding standard.

Nice work Kris!