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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Passing image object to method in template


Go to End


8 Posts   865 Views

Avatar
Rodskagg

Community Member, 26 Posts

4 April 2015 at 5:39am

I have a method called ImageTag set up in my Page which takes an image object as a parameter, and returns a string.

public function ImageTag($image) {
...
}

However, when I try to use this from my template, it doesn't work:

$ImageTag($MyImage)

I get an error saying "Trying to get property of non-object". My guess is that this is because I'm not actually passing the image object to the method, but rather the rendered image (i.e. a string with an image HTML tag). How do I access the actual image object from the template to pass that as a parameter to the method?

Avatar
catcherdev

Community Member, 9 Posts

4 April 2015 at 6:52am

Avatar
Rodskagg

Community Member, 26 Posts

4 April 2015 at 7:09am

Actually, I think that's the opposite is what I'm after. I need the pass in the actual Image object to my method, not the HTML tag, which is what's passed in at the moment.

Avatar
catcherdev

Community Member, 9 Posts

4 April 2015 at 7:17am

This method is on the Image class. You can use this instead of your method to get the tag for the object.

Avatar
Rodskagg

Community Member, 26 Posts

4 April 2015 at 7:18am

Edited: 04/04/2015 7:18am

Ah, I see your point. But I needed a custom image tag, so the default one will not do, I'm afraid.

Avatar
catcherdev

Community Member, 9 Posts

4 April 2015 at 7:22am

Edited: 04/04/2015 7:23am

The best solution is to create an Extension & apply it to Image, and define your getCustomTag() method there.
Then in your template, you can simply call $MyImage.CustomTag.

Avatar
Rodskagg

Community Member, 26 Posts

4 April 2015 at 7:24am

Yup, that's how I ended up solving it. I'm just curious if it's possible to solve the original problem - i.e. passing the actual image object to the method.

Avatar
catcherdev

Community Member, 9 Posts

4 April 2015 at 7:37am

Edited: 04/04/2015 7:39am

No, because it's already been converted to a string by Image's forTemplate method, as you discovered:
http://api.silverstripe.org/3.1/class-Image.html#_forTemplate

You could pass the ID to the method and Image::get()->byID($id) if you needed the object back in the controller.
Though at that point, you'd be better off appending the data to the object before it hits the template.

Extension solution is best, as you know.