Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "response"

Add inline javascript to the response

Allows you to define a script block in a template, and let it be included by the include_javascripts() helper after all js files, so that you can put it right before the body tag , to conform to the Yahoo performance best practice (see http://developer.yahoo.com/performance/rules.html#js_bottom).

First put this class in your /lib/response directory:

<?php
/*
*  LfmWebResponse
* @package    forcemolle
* @subpackage response
* @author     Jules Bernable <julius@laforcemolle.org>
*
*/
class LfmWebResponse extends sfWebResponse
{
  protected
    $inline_scripts = array();
 
  public function initialize(sfEventDispatcher $dispatcher, $options = array())
  {
    parent::initialize($dispatcher, $options);
 
    $this->inline_scripts = array_combine($this->positions, array_fill(0, count($this->positions), array()));
  }
 
  /**
   * Adds a script block to the current web response.
   *
   * @param string $script    The JavaScript code
   * @param string $id        A unique identifier for the script
   * @param string $position  Position
   */
  public function addInlineScript($script='', $position=sfWebResponse::LAST)
  {
    $this->validatePosition($position);
 
    $this->inline_scripts[$position][] = $script;
  }
 
  /**
   * Retrieves inline scripts from the current web response.
   *
   * By default, the position is sfWebResponse::ALL,
   * and the method returns all inline scripts ordered by position.
   *
   * @param  string $position  The position
   *
   * @return array An array of inline scripts
   */
  public function getInlineScripts($position = self::ALL)
  {
    if (self::ALL === $position)
    {
      $inline_scripts = array();
      foreach ($this->getPositions() as $position)
      {
        foreach ($this->inline_scripts[$position] as $script)
        {
          $inline_scripts[] = $script;
        }
      }
      return $inline_scripts;
    }
    else if (self::RAW === $position)
    {
      return $this->inline_scripts;
    }
 
    $this->validatePosition($position);
 
    return $this->inline_scripts[$position];
  }
 
}
 

Update your $sf_app_dir/config/factories.yml accordingly:

all:
  response:
    class: LfmWebResponse
 

Then you need to overwrite the symfony AssetHelper. First copy $sf_symfony_lib_dir/helper/AssetHelper.php to $sf_app_dir/lib/helper/AssetHelper.php (or to $sf_lib_dir/helper/AssetHelper.php if you want it enabled globally). Then replace the get_javascripts() function (line 492) by the following:

function get_javascripts()
{
  $response = sfContext::getInstance()->getResponse();
  sfConfig::set('symfony.asset.javascripts_included', true);
 
  $html = '';
  foreach ($response->getJavascripts() as $file => $options)
  {
    $html .= javascript_include_tag($file, $options);
  }
 
  use_helper('JavascriptBase');
  foreach ($response->getInlineScripts() as $script)
  {
    $html .= content_tag('script', javascript_cdata_section($script), array('type' => 'text/javascript'));
  }
 
  return $html;
}
 

Et voilà ! You can now add a script in a template this way:

<?php $sf_response->addInlineScript(<<<EOF
// Mootools FTW!
window.addEvent('domready', function()
{
  var foo = alert('BAR!');
});
EOF
);
?>
 

Your script block is now added after all js files added by include_javascripts() !

Please note that this is a basic implementation. You can not 'override' a script nor remove it afterwards.

by Jules Bernable on 2010-03-07, tagged javascript  performance  response 
(3 comments)

How to remove meta tags

You may find yourself wanting to remove default meta tags for specific modules within your application. This isn't possible through view.yml or module.yml, as we can only add or replace meta tags. We could add a removeMeta() method and call it from the module's pre- or postExecute() method, but as the default metas are added after action execution, that wouldn't work either.

The solution is to extend the sfWebResponse class, overriding the getMetas() method. This allows us to filter out unwanted tags without affecting special behaviour, e.g. for the title tag.

class myWebResponse extends sfWebResponse
{
  public function getMetas()
  {
    $meta_tags = $this->parameter_holder->getAll('helper/asset/auto/meta');
    if ($this->getContext()->getModuleName() == 'special_module' && array_key_exists('bad_meta', $meta_tags)) {
      unset($meta_tags['bad_meta']);
    }
    return $meta_tags;
  }
}
by James McGlinn on 2006-06-17, tagged metadata  response 
(1 comment)