Jump to content

Recommended Posts

Hi i use ps 1.4 and i want use a light php script for banner rotation like this:

 

<?php

$advert = array();

$advert[] = '<a href="#">Banner 1</a>';

$advert[] = '<a href="#">Banner 2</a>';

$advert[] = '<a href="#">Banner 3</a>';

shuffle($advert);

echo $advert[0];

?>

 

in index.tpl but i don't understand how do it because {php} or {include} not work in ps 1.4 version

 

I read to create smarty variable use assign function but HOW?

 

Thanks

Share this post


Link to post
Share on other sites

Hello,

the best way to do that :

 

1. Create a file /override/classes/FrontController.php with this code :

<?php
class FrontController extends FrontControllerCore
{
  public function init()
  {
  parent::init();

  $advert = array();
     $advert[] = '<a href="#">Banner 1</a>';
     $advert[] = '<a href="#">Banner 2</a>';
     $advert[] = '<a href="#">Banner 3</a>';
     shuffle($advert);
     self::$smarty->assign('my_adverts', $adverts);
  }
}

 

2. In your .tpl template file you can now use {$my_adverts}

 

Regards

Share this post


Link to post
Share on other sites

Raphaël Malié got right, in this case is the best way to use php code. As the extension name of file the says (.tpl) it's a template file - not controller ! :)

Share this post


Link to post
Share on other sites

Thank for information PrestArchitecte but when insert

{$my_adverts} in index.tpl under {$HOOK_HOME} or in header.tpl under <div id="center_column"> also inside a <div> tag nothing appear. Why?

I created frontcontroller.php with your code inside /override/class/

Thank

Share this post


Link to post
Share on other sites

In order to be sure that this class is correctly called, please add this code :

die('test');

after

self::$smarty->assign('my_adverts', $adverts); 

 

Do you see a blank page with "test" when you refresh your page in your browser ?

Share this post


Link to post
Share on other sites

Ok replace the overriden FrontController file by this content :

<?php
class FrontController extends FrontControllerCore
{
  public function displayContent()
  {
         parent::init();

         $advert = array();
     $advert[] = '<a href="#">Banner 1</a>';
     $advert[] = '<a href="#">Banner 2</a>';
     $advert[] = '<a href="#">Banner 3</a>';
     shuffle($advert);
     self::$smarty->assign('my_adverts', $adverts);

     parent::displayContent();
  }
}

 

Does this work ?

Share this post


Link to post
Share on other sites

Yes, you should do that :

<?php
class FrontController extends FrontControllerCore
{
  public function displayContent()
  {
         parent::init();

         $advert = array();
     $advert[] = '<a href="#">Banner 1</a>';
     $advert[] = '<a href="#">Banner 2</a>';
     $advert[] = '<a href="#">Banner 3</a>';

     self::$smarty->assign('my_adverts', $advert[array_rand($advert)]);

     parent::displayContent();
  }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More