Tuesday, March 10, 2009

Using the url attribute for an image

If you have an image field, and you have the url attribute for that field. Then you can access that field pretty easily to do some interesting things. Like for example, if in the event that there is no image we can set a default image using the same url attribute.

Say in your fields.ini of your delegate class you have:



The attribute url tells Xataface where in the application the images for this field are found. If you put your default image in this folder, then there are a number ways to make a default image appear. The idea is you don't want to have to hardcode the directory path into the code/template, but rather use that attribute in the fields.ini file (except for method 3).


  1. Method 1: Using the PHP function dirname()
    You can have an if statement which checks if there is an image, and if not then use the directory path of the folder containing your default image:


    if (!$record->display('image'))
    $default = dirname($record->display('image')).'/default.jpg';



  2. Method 2: Directly access the url attribute from the fields.ini file


    $fld = $record->_table->getField('image');
    $default = $fld['url'].'/default.jpg';


  3. Method 3: Move the image the default xataface image folder.
    Not really a method, but you can just place the default image in another folder and rely on Xataface's environment variables to help you. Say you want to display a default image in the event there is no image. You do this in the smarty template:


    {if $row->val('image') }
    <img src="{$row->display('image')}?max_height=100&max_width=100" />
    {else}
    <img src="{$ENV.DATAFACE_SITE_URL}/images/default_tire.jpg?max_height=100&max_width=100" />
    {/if}



No comments: