Jump to content

php script in index.tpl


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

Link to comment
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

Link to comment
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

Link to comment
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 ?

Link to comment
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();
  }
}

Link to comment
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...