2864 Posts in 732 Topics by 700 members
Template Questions
SilverStripe Forums » Template Questions » Image.SetWidth(150) Link
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
| Go to End | Next > | |
| Author | Topic: | 3308 Views |
-
Image.SetWidth(150) Link

25 February 2009 at 9:38am
Hello,
How do I get the link to a resized image in a template? It would be nice to be able to do something like:
Image.SetWidth(150).Link
However, I can't do that as Image.SetWidth(150) always returns HTML.(In case you're wondering, I'm building a desktop background download page where the background image needs to be offered in several resolutions. I would like to auto-generate those variations using image resizing functionality.)
Thank you,
Ben -
Re: Image.SetWidth(150) Link

25 February 2009 at 11:45am
More on this....
It is possible to extend Image by adding convenience methods to provide the desired data. Example:class WallpaperResourceImage extends Image {
public function ResizedURL($width) {
return $this->SetWidth($width)->URL();
}
}The downside with this approach is that referencing the resized image to retrieve different pieces of data in the template becomes cumbersome. Instead of being able to do something simple like this --
Image.SetWidth(150).Link
Image.SetWidth(150).getHeight
-- we have to add a method to our Image extension (in this case, WallpaperResourceImage) for every piece of data we want. -
Re: Image.SetWidth(150) Link

25 February 2009 at 7:45pm Last edited: 25 February 2009 7:46pm
Strangely, I came across this very same problem today, trying to set some page-specific CSS.
Writing a class is a very messy solution because it also means the theme and mysite folders are more coupled. This syling information should definitely be in the themes directory. If anyone can offer any ideas, I would also love to hear them.
-
Re: Image.SetWidth(150) Link

5 March 2009 at 12:07pm
More on this:
The template parser (in sapphire\core\SSViewer.php) uses regular expressions to convert template method/property calls into PHP. These regexes look for a fixed combination of call signatures--i.e. "Property.Subproperty", "Property(one_argument).Subproperty", "Property.Subproperty(one_argument)", etc. The signature of "Image.SetWidth(150).Link " does not match any of the defined call signatures. However, the signature of "Image.SetWidth(150)" does match one of the signatures. So, the parser converts "Image.SetWidth(150)" into PHP (which then is rendered as HTML) and then outputs ".URL" after it.
A draft patch has been proposed which will remove the limitation of having a fixed list of allowed call signatures.
-
Re: Image.SetWidth(150) Link

5 March 2009 at 4:24pm
This is a bit clumsy but should work
<% control Image %>
<% control SetWidth(150) %>
<a href="$Link"><img src="$URL" alt="$Title" /></a>
<% end_control %>
<% end_control %> -
Re: Image.SetWidth(150) Link

19 March 2009 at 8:21am
The above-mentioned patch is attached to ticket #3738. Testers are needed to try it out.
-
Re: Image.SetWidth(150) Link

19 March 2009 at 1:27pm
Nice work bgribaudo looks patch sounds like an awesome addition to the SSViewer!
| 3308 Views | ||
| Go to Top | Next > |



