Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » Using .SetWidth() with a static image?

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

Page: 1
Go to End
Author Topic: 1225 Views
  • pinkp
    Community Member
    168 Posts

    Using .SetWidth() with a static image? Link to this post

    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

    * 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");



  • baba-papa
    Community Member
    279 Posts

    Re: Using .SetWidth() with a static image? Link to this post

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


Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.