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.

Template Questions /

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

inline css in template


Go to End


4 Posts   3475 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 :)