Development

Changeset 11548

You must first sign up to be able to contribute.

Changeset 11548

Show
Ignore:
Timestamp:
09/14/08 22:00:46 (2 months ago)
Author:
FabianLange
Message:

[1.2] image_tag helper will no longer guess alt attribute unless sf_compat_10 is on. Added alt_title option to set both attributes in one go. Added unit tests and documentation. fixes #1391

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r11542 r11548  
    11241124The old behavior still works without changing anything to your code. 
    11251125 
     1126Image helper 
     1127------------ 
     1128 
     1129In symfony 1.0 and 1.1 the `image_tag` helper would generate the `alt` 
     1130attribute of the img-tag from the filename. This now only happens if 
     1131`sf_compat_10` is on. The new behaviour eases finding of unset alt attributes 
     1132using a (x)html validator. As a bonus, there is now a `alt_title` option that 
     1133will set alt and title attribute to the same value, which is useful for cross 
     1134browser tooltips. 
     1135 
    11261136View Configuration 
    11271137------------------ 
  • branches/1.2/lib/helper/AssetHelper.php

    r11102 r11548  
    321321  } 
    322322 
    323   if (!isset($options['alt'])) 
     323  if (isset($options['alt_title'])) 
     324  { 
     325    //set as alt and title but do not overwrite explicitly set 
     326    if(!isset($options['alt'])) $options['alt'] = $options['alt_title']; 
     327    if(!isset($options['title'])) $options['title'] = $options['alt_title']; 
     328    unset($options['alt_title']); 
     329  } 
     330 
     331  if (!isset($options['alt']) && sfConfig::get('sf_compat_10')) 
    324332  { 
    325333    $path_pos = strrpos($source, '/'); 
  • branches/1.2/test/unit/helper/AssetHelperTest.php

    r9335 r11548  
    1616require_once(dirname(__FILE__).'/../../../lib/helper/AssetHelper.php'); 
    1717 
    18 $t = new lime_test(54, new lime_output_color()); 
     18$t = new lime_test(57, new lime_output_color()); 
    1919 
    2020class myRequest 
     
    5959$t->is(_compute_public_path('foo?foo=bar', 'css', 'css'), '/css/foo.css?foo=bar', '_compute_public_path() takes into account query strings'); 
    6060 
     61$compatMode = sfConfig::get('sf_compat_10'); 
     62sfConfig::set('sf_compat_10', false); 
    6163// image_tag() 
    6264$t->diag('image_tag()'); 
    6365$t->is(image_tag(''), '', 'image_tag() returns nothing when called without arguments'); 
    64 $t->is(image_tag('test'), '<img src="/images/test.png" alt="Test" />', 'image_tag() takes an image name as its first argument'); 
    65 $t->is(image_tag('test.png'), '<img src="/images/test.png" alt="Test" />', 'image_tag() can take an image name with an extension'); 
    66 $t->is(image_tag('/images/test.png'), '<img src="/images/test.png" alt="Test" />', 'image_tag() can take an absolute image path'); 
    67 $t->is(image_tag('/images/test'), '<img src="/images/test.png" alt="Test" />', 'image_tag() can take an absolute image path without extension'); 
    68 $t->is(image_tag('test.jpg'), '<img src="/images/test.jpg" alt="Test" />', 'image_tag() can take an image name with an extension'); 
     66$t->is(image_tag('test'), '<img src="/images/test.png" />', 'image_tag() takes an image name as its first argument'); 
     67$t->is(image_tag('test.png'), '<img src="/images/test.png" />', 'image_tag() can take an image name with an extension'); 
     68$t->is(image_tag('/images/test.png'), '<img src="/images/test.png" />', 'image_tag() can take an absolute image path'); 
     69$t->is(image_tag('/images/test'), '<img src="/images/test.png" />', 'image_tag() can take an absolute image path without extension'); 
     70$t->is(image_tag('test.jpg'), '<img src="/images/test.jpg" />', 'image_tag() can take an image name with an extension'); 
    6971$t->is(image_tag('test', array('alt' => 'Foo')), '<img alt="Foo" src="/images/test.png" />', 'image_tag() takes an array of options as its second argument to override alt'); 
    70 $t->is(image_tag('test', array('size' => '10x10')), '<img src="/images/test.png" alt="Test" height="10" width="10" />', 'image_tag() takes a size option'); 
    71 $t->is(image_tag('test', array('absolute' => true)), '<img src="http://localhost/images/test.png" alt="Test" />', 'image_tag() can take an absolute parameter'); 
    72 $t->is(image_tag('test', array('class' => 'bar')), '<img class="bar" src="/images/test.png" alt="Test" />', 'image_tag() takes whatever option you want'); 
     72$t->is(image_tag('test', array('size' => '10x10')), '<img src="/images/test.png" height="10" width="10" />', 'image_tag() takes a size option'); 
     73$t->is(image_tag('test', array('absolute' => true)), '<img src="http://localhost/images/test.png" />', 'image_tag() can take an absolute parameter'); 
     74$t->is(image_tag('test', array('class' => 'bar')), '<img class="bar" src="/images/test.png" />', 'image_tag() takes whatever option you want'); 
     75$t->is(image_tag('test', array('alt_title' => 'Foo')), '<img src="/images/test.png" alt="Foo" title="Foo" />', 'image_tag() takes an array of options as its second argument to create alt and title'); 
     76$t->is(image_tag('test', array('alt_title' => 'Foo', 'title' => 'Bar')), '<img title="Bar" src="/images/test.png" alt="Foo" />', 'image_tag() takes an array of options as its second argument to create alt and title'); 
     77sfConfig::set('sf_compat_10', true); 
     78$t->is(image_tag('test'), '<img src="/images/test.png" alt="Test" />', 'image_tag() creates alt attribute from filename if sf_compat_10 is on'); 
     79 
     80sfConfig::set('sf_compat_10', $compatMode); 
    7381 
    7482// stylesheet_tag() 
  • doc/branches/1.2/book/09-Links-and-the-Routing-System.txt

    r11181 r11548  
    345345>Chapter 7 introduced the asset helpers `image_tag()`, `stylesheet_tag()`, and `javascript_include_ tag()`, which allow you to include an image, a style sheet, or a JavaScript file in the response. The paths to such assets are not processed by the routing system, because they link to resources that are actually located under the public web directory. 
    346346> 
    347 >You don't need to mention a file extension for an asset. Symfony automatically adds `.png`, `.js`, or `.css` to an image, JavaScript, or style sheet helper call. Also, symfony will automatically look for those assets in the `web/images/`, `web/js/`, and `web/css/` directories. Of course, if you want to include a specific file format or a file from a specific location, just use the full file name or the full file path as an argument. And don't bother to specify an `alt` attribute if your media file has an explicit name, since symfony will determine it for you. 
     347>You don't need to mention a file extension for an asset. Symfony automatically adds `.png`, `.js`, or `.css` to an image, JavaScript, or style sheet helper call. Also, symfony will automatically look for those assets in the `web/images/`, `web/js/`, and `web/css/` directories. Of course, if you want to include a specific file format or a file from a specific location, just use the full file name or the full file path as an argument. 
     348>And don't bother to specify both an `alt` and `title` attribute, as `alt_title` will set both attributes to the same value, which is useful for cross browser tooltips. Note that as of symfony 1.2 the `alt` attribute is no longer autoguessed from the filename to ease finding missing tags using a validator (to enable this again, turn `sf_compat_10` on). 
    348349> 
    349350>     [php] 
    350 >     <?php echo image_tag('test') ?> 
    351 >     <?php echo image_tag('test.gif') ?> 
    352 >     <?php echo image_tag('/my_images/test.gif') ?> 
     351>     <?php echo image_tag('test', 'alt=Test') ?> 
     352>     <?php echo image_tag('test.gif', 'title=Test') ?> 
     353>     <?php echo image_tag('/my_images/test.gif', 'alt_title=Test') ?> 
    353354>      => <img href="/images/test.png" alt="Test" /> 
    354 >         <img href="/images/test.gif" alt="Test" /> 
    355 >         <img href="/my_images/test.gif" alt="Test" /> 
     355>         <img href="/images/test.gif" title="Test" /> 
     356>         <img href="/my_images/test.gif" alt="Test" title="Test" /> 
    356357> 
    357358>To fix the size of an image, use the `size` attribute. It expects a width and a height in pixels, separated by an `x`. 
     
    359360>     [php] 
    360361>     <?php echo image_tag('test', 'size=100x20')) ?> 
    361 >      => <img href="/images/test.png" alt="Test" width="100" height="20"/> 
     362>      => <img href="/images/test.png" width="100" height="20"/> 
    362363> 
    363364>If you want the asset inclusion to be done in the `<head>` section (for JavaScript files and style sheets), you should use the `use_stylesheet()` and `use_javascript()` helpers in your templates, instead of the `_tag()` versions in the layout. They add the asset to the response, and these assets are included before the `</head>` tag is sent to the browser.