Development

Changeset 8483

You must first sign up to be able to contribute.

Changeset 8483

Show
Ignore:
Timestamp:
04/16/08 18:43:54 (4 months ago)
Author:
fabien
Message:

added some helpers to ease the use of dynamic javascripts and stylesheets (actions with a sf_format of sf or css)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/helper/AssetHelper.php

    r7757 r8483  
    536536  echo get_stylesheets(); 
    537537} 
     538 
     539/** 
     540 * Returns a <script> include tag for the given internal URI. 
     541 * 
     542 * The helper automatically adds the sf_format to the internal URI, so you don't have to. 
     543 * 
     544 * @param  string  The internal URI for the dynamic javascript 
     545 * @param  Boolean Whether to generate an absolute URL 
     546 * @param  array   An array of options (absolute) 
     547 * 
     548 * @return string  XHTML compliant <script> tag(s) 
     549 * @see    javascript_include_tag  
     550 */ 
     551function dynamic_javascript_include_tag($uri, $absolute = false, $options = array()) 
     552{ 
     553  $options['raw_name'] = true; 
     554 
     555  return javascript_include_tag(_dynamic_path($uri, 'js', $absolute), $options); 
     556} 
     557 
     558/** 
     559 * Adds a dynamic javascript to the response object. 
     560 * 
     561 * The first argument is an internal URI. 
     562 * The helper automatically adds the sf_format to the internal URI, so you don't have to. 
     563 * 
     564 * @see sfResponse->addJavascript() 
     565 */ 
     566function use_dynamic_javascript($js, $position = '', $options = array()) 
     567{ 
     568  $options['raw_name'] = true; 
     569 
     570  return use_javascript(_dynamic_path($js, 'js'), $position, $options); 
     571} 
     572 
     573/** 
     574 * Adds a dynamic stylesheet to the response object. 
     575 * 
     576 * The first argument is an internal URI. 
     577 * The helper automatically adds the sf_format to the internal URI, so you don't have to. 
     578 * 
     579 * @see sfResponse->addStylesheet() 
     580 */ 
     581function use_dynamic_stylesheet($css, $position = '', $options = array()) 
     582{ 
     583  $options['raw_name'] = true; 
     584 
     585  return use_stylesheet(_dynamic_path($css, 'css'), $position, $options); 
     586} 
     587 
     588function _dynamic_path($uri, $format, $absolute = false) 
     589{ 
     590  return url_for($uri.(false === strpos($uri, '?') ? '?' : '&').'sf_format='.$format, $absolute); 
     591} 
  • branches/1.1/test/unit/helper/AssetHelperTest.php

    r7757 r8483  
    1616require_once(dirname(__FILE__).'/../../../lib/helper/AssetHelper.php'); 
    1717 
    18 $t = new lime_test(45, new lime_output_color()); 
     18$t = new lime_test(53, new lime_output_color()); 
    1919 
    2020class myRequest 
     
    3838} 
    3939 
    40 $context = sfContext::getInstance(array('request' => 'myRequest', 'response' => 'sfWebResponse')); 
     40class myController 
     41
     42  public function genUrl($parameters = array(), $absolute = false) 
     43  { 
     44    return ($absolute ? '/' : '').$parameters; 
     45  } 
     46
     47 
     48$context = sfContext::getInstance(array('request' => 'myRequest', 'response' => 'sfWebResponse', 'controller' => 'myController')); 
    4149 
    4250// _compute_public_path() 
     
    159167  '<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />'."\n".'<link rel="stylesheet" type="text/css" media="screen" href="/css/style2.css" />'."\n", 
    160168  'get_stylesheets() returns all the stylesheets previously added by use_stylesheet()'); 
     169 
     170// _dynamic_path() 
     171$t->diag('_dynamic_path()'); 
     172$t->is(_dynamic_path('module/action', 'js'), 'module/action?sf_format=js', '_dynamic_path() converts an internal URI to a URL'); 
     173$t->is(_dynamic_path('module/action?key=value', 'js'), 'module/action?key=value&sf_format=js', '_dynamic_path() converts an internal URI to a URL'); 
     174$t->is(_dynamic_path('module/action', 'js', true), '/module/action?sf_format=js', '_dynamic_path() converts an internal URI to a URL'); 
     175 
     176// dynamic_javascript_include_tag() 
     177$t->diag('dynamic_javascript_include_tag()'); 
     178$t->is(dynamic_javascript_include_tag('module/action'), '<script type="text/javascript" src="module/action?sf_format=js"></script>'."\n", 'dynamic_javascript_include_tag() returns a tag relative to the given action'); 
     179$t->is(dynamic_javascript_include_tag('module/action', true), '<script type="text/javascript" src="/module/action?sf_format=js"></script>'."\n", 'dynamic_javascript_include_tag() takes an absolute boolean as its second argument'); 
     180$t->is(dynamic_javascript_include_tag('module/action', true, array('class' => 'foo')), '<script type="text/javascript" src="/module/action?sf_format=js" class="foo"></script>'."\n", 'dynamic_javascript_include_tag() takes an array of HTML attributes as its third argument'); 
     181 
     182$context->response = new sfWebResponse($context->getEventDispatcher()); 
     183 
     184// use_dynamic_javascript() 
     185$t->diag('use_dynamic_javascript()'); 
     186use_dynamic_javascript('module/action'); 
     187$t->is(get_javascripts(), 
     188  '<script type="text/javascript" src="module/action?sf_format=js"></script>'."\n", 
     189  'use_dynamic_javascript() register a dynamic javascript in the response' 
     190); 
     191 
     192// use_dynamic_stylesheet() 
     193$t->diag('use_dynamic_stylesheet()'); 
     194use_dynamic_stylesheet('module/action'); 
     195$t->is(get_stylesheets(), 
     196  '<link rel="stylesheet" type="text/css" media="screen" href="module/action?sf_format=css" />'."\n",  
     197  'use_dynamic_stylesheet() register a dynamic stylesheet in the response' 
     198);