17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1302 Views |
-
Showing an image in a template with a 'src' containing a $Variable out of a Control

6 August 2008 at 8:34pm Last edited: 6 August 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.
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

6 August 2008 at 8:52pm Last edited: 6 August 2008 9:02pm
Not sure what you mean by that and why you wanna do that?
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

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".
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

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
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

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
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

6 August 2008 at 9:37pm
Yeah right Will, I totally forgot about good old {}
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

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.
-
Re: Showing an image in a template with a 'src' containing a $Variable out of a Control

6 August 2008 at 9:40pm
Ok the {} solution is the better one, thanks
| 1302 Views | ||
|
Page:
1
|
Go to Top |


