Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using .SetWidth() with a static image?


Go to End


2 Posts   1999 Views

Avatar
pinkp

Community Member, 182 Posts

25 March 2010 at 3:45am

Edited: 25/03/2010 3:47am

OK I've tried many combinations but can't work this out so any help appreciated.

I am creating a Rotate / Random Image module and the image source is a php file.
I wish to allow the user to set the width of the image generated as there are many I can't have it fixed as it will distort them.
Also if they only choose and set the width then IE doesn't like it, as there is no height set. I've found using SetWidth() helps get around this.
I also wish to be able to control the alt / title text.

I currently use this for setting the alt and width for other pages:

<% if HomeLeftImage %>
   						<% control HomeLeftImage.SetWidth(150) %>
      					<img src="$URL"<% end_control %> alt="$HomeLeftImageTitle"/><% else %><% end_if %>

This currently works in FF to get the random image and set its width and alt etc

<img src="image_rotate/code/rotate.php" width="{$ImageRotateWidth}px" alt="<% if ImageRotateTitle %>$ImageRotateTitle<% else %><% end_if %>" title="<% if ImageRotateTitle %>$ImageRotateTitle<% else %><% end_if %>"/>

What I want is something more like this but this throws up errors....?

<% control ImageRotate.SetWidth({$ImageRoateWidth}) %>
      					<img src="image_rotate/code/rotate.php"<% end_control %> alt="<% if ImageRotateTitle %>$ImageRotateTitle<% else %><% end_if %>" title="<% if ImageRotateTitle %>$ImageRotateTitle<% else %><% end_if %>"/>

This is my PHP if helpful

<?php
/**
 * Defines the ImageRotatePage page type
 */

class ImageRotatePage extends Page {
	
	   static $icon = "image_rotate/images/treeicons/imagerotate";

   static $db = array(
    'ImageRotateTitle'  => 'Text',
	'ImageRotateWidth'  => 'Int'

   );
   
   
   static $has_one = array(

	 );
   
   	static $defaults = array (
	'ImageRotateTitle'  => 'My Title',
	'ImageRotateWidth'  => '300',

	);

  function getCMSFields()
   {
	   
      $fields = parent::getCMSFields();    
      $fields->addFieldToTab("Root.Content.Configuration", new TextField('ImageRotateTitle','Image Title'));
      $fields->addFieldToTab("Root.Content.Configuration", new NumericField('ImageRotateWidth','Image Width (pixels)'));
 
	  

return $fields;
 }
}
 
class ImageRotatePage_Controller extends Page_Controller {
	 
   function init() {
      if(Director::fileExists(project() . "/css/imagerotate.css")) {
         Requirements::css(project() . "/css/imagerotate.css");
      }else{
         Requirements::css("image_rotate/css/imagerotate.css");
      }
      parent::init();	
   }
 
}
?>

thanks!

Avatar
baba-papa

Community Member, 279 Posts

25 March 2010 at 6:45am

As far as i know you can´t use variables as a value in template methods:

ImageRotate.SetWidth({$ImageRoateWidth})