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.

Archive /

Our old forums are still available as a read-only archive.

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

Showing an image in a template with a 'src' containing a $Variable out of a Control


Go to End


8 Posts   1771 Views

Avatar
FlorianH

Community Member, 33 Posts

6 August 2008 at 8:34pm

Edited: 06/08/2008 8:37pm

Hello Community,

I've got a problem with the templates again. I'll show you some pseudocode first which should explain my Problem:

Template.ss

<% control MyControl %>
<img src="blablabla/$MySQLField.jpg">
<% end_control %>

Of course the code looks for the SQL Field called MySQLField.jpg which is not existant since it's called MySQLField. So my question is, is there something like an escape-symbol (or whatever it's called, sorry ;)) which helps me with just looking up the MySQLField and add a '.jpg' behind?

Thanks in advance

Florian H.

Avatar
Phalkunz

Community Member, 69 Posts

6 August 2008 at 8:52pm

Edited: 06/08/2008 9:02pm

Not sure what you mean by that and why you wanna do that?

Avatar
FlorianH

Community Member, 33 Posts

6 August 2008 at 9:00pm

I've got a Field in a DataObject Table which contains a number, additionally there is a folder which has several image files in there called (for example) "123456789.jpg". The number itself is actually a ISBN and the pictures are book titles (related to the ISBN). So I grab the ISBN out of the database and look up the book picture called "$isbn.jpg".

Avatar
Phalkunz

Community Member, 69 Posts

6 August 2008 at 9:20pm

You might wanna look at this tut http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management

In your case, you need to use $has_one in Template.php . Assuming the DataObject called Book.php and it has a field called ISBN. In Template.php:

...
$has_one = array('Book'=>'Book');
...

So, in Template.ss

...
<% control Book %>
   <img src="path_to_folder/$ISBN.jpg">
<% end_control %>
...

This should work :)

Avatar
Willr

Forum Moderator, 5523 Posts

6 August 2008 at 9:35pm

the template parser isnt too clever - $ISBN.jpg it will look for the ISBN.jpg field. You need to wrap the var in {} if you want it to work eg

<img src="path_to_folder/{$ISBN}.jpg

Avatar
Phalkunz

Community Member, 69 Posts

6 August 2008 at 9:37pm

Yeah right Will, I totally forgot about good old {} :)

Avatar
FlorianH

Community Member, 33 Posts

6 August 2008 at 9:40pm

I already got a working relation to the books, my problem is just that "$ISBN.jpg" doesn't work, since there is no Field called "ISBN.jpg" or function called "jpg". "$ISBN .jpg" works, "($ISBN).jpg" works, "$ISBN\.jpg" works etc.

Think of the ".XML" function, you can use something like "$Content.XML" ect. but there is no ".jpg" in the core. I just found a solution

<img src="folder/<% control isbn %>$XML<% end_control %>.jpg">

This helped me out.

Avatar
FlorianH

Community Member, 33 Posts

6 August 2008 at 9:40pm

Ok the {} solution is the better one, thanks :)