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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

inline css in template


Go to End


4 Posts   2664 Views

Avatar
markus85

Community Member, 12 Posts

15 November 2009 at 2:11am

Hi out there,

maybe some one can help me.

I want to add an random image to my header. These images are in the image-folder from my template.

How can I access these files from the template --> Page.ss ?

I just inserted some lines of code and it works fine but there is no image :( I think the relative path is wrong.
So my question is:

How can I access these files from the Page.ss ?
Is there another possibility to get inline-css code into a template ?

Greets

Avatar
ramu

Community Member, 15 Posts

15 November 2009 at 2:22am

Edited: 15/11/2009 2:23am

Hi markus85,

Please find the sample code to get the image from the folder

function getAllFiles($directory='') {
		$result = array();
		$folder = Director::baseFolder() . '/'.$directory.'thumb/';
		$thumbdir = $directory.'thumb/';
   	    $handle =  opendir($folder);
		$directorylist='';
		 while ($datei = readdir($handle))
		 {
			  if (($datei != '.') && ($datei != '..') && ($datei != 'Thumbs.db'))
			  {
				   $file = $thumbdir.$datei;
				   $abpath = $folder.$datei;
				   if (!is_dir($file)) {
						$result[filemtime($abpath)] = $file;
				   }
			  }
		 }
		 closedir($handle);
		 foreach ($result as $val) {
		 	$filename = basename($val);
		 	 $directorylist .= '<div><a href="'.$directory.$filename.'"><img src="'.$val.'" /></a></div>';
		 }
  		 return $directorylist;
}

Avatar
bummzack

Community Member, 904 Posts

15 November 2009 at 2:48am

Edited: 15/11/2009 2:48am

Inline css in the template should be fine. If you want the path to the template folder, you can either enter the full path (relative to the site-base) or use the $ThemeDir variable in your template. Something like this:

<style type="text/css">
#MyHeader {
background: url($ThemeDir/images/myHeader.jpg) no-repeat left top;
}
</style>

Avatar
markus85

Community Member, 12 Posts

15 November 2009 at 7:06am

thx for the answers.
Didn´t know about that variable $ThemeDir.

Works fine :)