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

Newbie - images?


Go to End


5 Posts   1906 Views

Avatar
staple

Community Member, 7 Posts

13 March 2009 at 2:02pm

Hiya,

I want to add an image to my page.ss - what is the best syntax to use..

- Should $ImagenameImage print out a html tag for an image, if so what dir should it live in?
- Where should I put images that part of a template but not called from CSS?

Thanks

Avatar
Carbon Crayon

Community Member, 598 Posts

13 March 2009 at 2:13pm

Edited: 13/03/2009 2:13pm

Hi staple

I'm not sure I totally understand you question.

There are a couple of ways to add images in your tempalte, the most common being;

$Image - this will add a complete image HTML like <img src="myImage.jpg" alt="Image name" >

$Image.URL - this will return the URL so that you can use your own tag like so: <img id="img" src="$Image.URL" alt="some proper text" >

you can also resize images in the template:

$Image.SetWidth(100)

Unfortunately you cant then add .URL on the end, so if you want to resize and only use a url then you need to do somthing like this:

<% control Image %>
<% control CroppedImage(100,100) %>
<img src="$URL" alt="some text" >
<% end_control %>
<% end_control %>

Hope that helps :)

Avatar
staple

Community Member, 7 Posts

13 March 2009 at 3:07pm

Um...hmm..

Thanks for the reply.. I guess I mean..

I have an image in '/themes/MyTheme/images/theMoon.jpg' but in order to put it in the template I want the path to work properly (I am rewriting my baseurl). In cakePHP I would have used the image helper...

echo $html->image('title', 'path')

...and it would automatically add the right root url.

Does that make sense? :-)

Avatar
UncleCheese

Forum Moderator, 4102 Posts

13 March 2009 at 3:24pm

If the image is part of your template, you can call it like Aram said, only use the $ThemeDir global var to refer to the current theme directory. If the image is part of the model, however, you can call it using its fieldname.. e.g. $MyImage, which will render an image tag for you. Otherwise, for a custom tag, you can do img src="$MyImage.URL" alt="" etc...

Helpers, despite all their admirable attempts to streamline development, break the MVC pattern and should be avoided when possible.

Avatar
JennieViv

Community Member, 1 Post

18 March 2009 at 5:16pm

Im a newbie too, and I am having the same trouble.. :) Thanks for posting.. So psyched to be here!