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.

Archive

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

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

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


Reply

8 Posts   1446 Views

Avatar
FlorianH

6 August 2008 at 8:34pm (Last edited: 6 August 2008 8:37pm), Community Member, 33 Posts

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

6 August 2008 at 8:52pm (Last edited: 6 August 2008 9:02pm), 69 Posts

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

Avatar
FlorianH

6 August 2008 at 9:00pm Community Member, 33 Posts

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

6 August 2008 at 9:20pm 69 Posts

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

6 August 2008 at 9:35pm Forum Moderator, 5511 Posts

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

6 August 2008 at 9:37pm 69 Posts

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

Avatar
FlorianH

6 August 2008 at 9:40pm Community Member, 33 Posts

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

6 August 2008 at 9:40pm Community Member, 33 Posts

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