Thread: PHP in temlate
View Single Post
  #6  
Old 09-22-2010, 07:40 AM
 
geckoday geckoday is offline
 

X-Wizard
  
Join Date: Aug 2005
Posts: 1,073
 

Default Re: PHP in temlate

Two ways. The easiest is to just do this in the template like this:
Code:
{assign var=video_array value='/home/minfinge/public_html/littlepuppiesonline.com/skin1/customer/main/YourVideoList.txt'|file} {assign var=video_rand value=$video_array|@array_rand} <object width="200" height="137"><param name="movie" value="http://www.youtube.com/v/{$video_array.$video_rand|trim}"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/{$video_array.$video_rand|trim}" type="application/x-shockwave-flash" wmode="transparent" width="200" height="137"></embed></object>

That still puts code into the template and you'd end up duplicating it everywhere if you use it in multiple places. I prefer to use Smarty plugins for this kind of stuff like this:

PHP Code:
<?php
if (!defined('XCART_START')) { header("Location: ../../../"); die("Access denied"); }
## -----------------------------------------------------------------------------
## Smarty plugin "random_video"
## Purpose: Retrieve random vidoe name for display
##
##
## -----------------------------------------------------------------------------
##
## Changelog:
##
## 2010-09-22 Created
##
##
## -----------------------------------------------------------------------------
##
## Preparations
## ------------
## Uses the following file:
##   skin1/customer/main/YourVideoList.txt - list of videos, one per line
## 
##
## Sample usage
## ------------
## {random_video result="video_rand"}
## <object width="200" height="137"><param name="movie" value="http://www.youtube.com/v/{$video_rand}"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/{$video_rand}" type="application/x-shockwave-flash" wmode="transparent" width="200" height="137"></embed></object>
## 
##
## Parameters
## ----------
##
## result [string] (required)
##      Smarty variable to return result string in.  Result is a random line form the video file
##      trimmed and ready for substitution into HTML.
## 
## 
## -----------------------------------------------------------------------------

function smarty_function_random_video($params, &$smarty) {

  
$required_params = array('result');

  foreach(
$required_params as $_key => $reqd_param) {
    if (!isset(
$params[$reqd_param]))
      
$smarty->trigger_error("random_video: required attribute '$reqd_param' not passed"E_USER_ERROR);
  }

  
$result    '';
  foreach(
$params as $_key => $_val) {
    switch(
$_key) {
      case 
'result':
          if (!
is_array($_val)) {
              if (
$_val <> '')
                $
$_key $_val;
              else
                
$smarty->trigger_error("random_video: '$_key' cannot be an empty string"E_USER_ERROR);
          } else
              
$smarty->trigger_error("random_video: '$_key' cannot be an array"E_USER_ERROR);
          break;

      default:
          
$smarty->trigger_error("random_video: attribute '$_key' not recognized"E_USER_NOTICE);
          break;
    }
  }

  
$video_file $smarty->template_dir.DIRECTORY_SEPARATOR."customer/main/YourVideoList.txt";
  
  if (!
is_file($video_file))
      
$smarty->trigger_error("random_video: video list file '$video_file' is missing"E_USER_NOTICE);
      
  
$video_array file($video_file);
  if (
count($video_array) < 1)
      
$smarty->trigger_error("random_video: video list file '$video_file' is empty"E_USER_NOTICE);
      
  
$smarty->assign($resulttrim($video_array[array_rand($video_array)]));

}
Save this in include/templater/plugins/function.random_video.php and use it like the code comments show. I would probably add a parameter to select the video file to it so it can be used in multiple places for different lists of videos but I'll leave that as an exercise for the reader.
__________________
Manuka Bay Company
X-Cart Version 4.0.19 [Linux]

UGG Boots and other fine sheepskin products
http://www.snowriver.com
Reply With Quote