Jump to content

Create variable background image - help


Recommended Posts

Hello, 

 

I am trying to create a variable background with image 1.jpg,2.jpg,3.jpg,4.jpg etc..

 

First what I do a function inside FrontController.php like this:

public function randomBackground()
	{
	//add variable background
	$bg = array('_1.jpg', '_2.jpg', '_3.jpg' ); // array of filenames

	$i = rand(0, count($bg)-1); // generate random number size of the array
	$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
		$this->context->smarty->assign(array(
		'background_random' => $selectedBg,
	));	
	}

Second I style and change header.tpl (..yourtheme/header.tpl) like this:

<div class="columns-container" style="background: url('../themes/default-bootstrap/img/bg/{$background_random|escape:'html':'UTF-8'}') center no-repeat fixed #FFF;background-size: cover;">

But it does not work. If I load a page and view a console I got this:

<div class="columns-container" style="background: url('../themes/default-bootstrap/img/bg/
Notice: Undefined index: background_random in /var/www/clients/client1/web1/web/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 184

Notice: Trying to get property of non-object in /var/www/clients/client1/web1/web/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 184
') center no-repeat fixed #FFF;background-size: cover;">........</div>

Any idea, how to fix it? 

 

Thank you

Shaft :-)

Link to comment
Share on other sites

Did you run that custom method you created? Say, inside initContent

I think so no. Becose this is first time, what I create a function and apply a varialbe to .tpl. So I do not know, what excatly run if I create it. 

Can you help me please, what run to register new function and apply to .tpl?

 

Thank you,

Regards,

Shaft :-)

Link to comment
Share on other sites

×
×
  • Create New...