X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   PHP in temlate (https://forum.x-cart.com/showthread.php?t=55624)

chapel 09-20-2010 12:17 PM

PHP in temlate
 
how can I run php in the templates

balinor 09-20-2010 12:41 PM

Re: PHP in temlate
 
{php}

your php here

{/php}

Jon 09-20-2010 02:30 PM

Re: PHP in temlate
 
Though it's not recommended.

balinor 09-20-2010 02:47 PM

Re: PHP in temlate
 
Right, forgot to mention, best to keep PHP in the php files.

minfinger 09-22-2010 05:05 AM

Re: PHP in temlate
 
That being said, what's the best way to make this work?
Code:

<?php
// Build an array from the list of YouTube videos
// Replace YourVideoList.txt with the path to your text file
// This will likely be something like /home/accountname/public_html/foldername/etc
$video_array = file('/home/minfinge/public_html/littlepuppiesonline.com/skin1/customer/main/YourVideoList.txt');
// Randomly pick one video from the array
$video = $video_array[rand(0, count($video_array) - 1)];
$video = trim($video);
?>
<object width="200" height="137"><param name="movie" value="http://www.youtube.com/v/<?php echo $video;?>"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/<?php echo $video;?>" type="application/x-shockwave-flash" wmode="transparent" width="200" height="137"></embed></object>


I'm trying to get it to display Videos Randomly based on this site.
http://www.jakeludington.com/youtube/20070615_how_to_randomize_youtube_videos_with_php. html

geckoday 09-22-2010 07:40 AM

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.

minfinger 09-22-2010 08:32 AM

Re: PHP in temlate
 
OMG that's awesome!!!

www.littlepuppiesonline.com Don't let your wife or gf or both look at this site though. lol

You're a life saver Ralph!


All times are GMT -8. The time now is 07:25 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.