Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

PHP in temlate

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 09-20-2010, 12:17 PM
 
chapel chapel is offline
 

Advanced Member
  
Join Date: Oct 2005
Posts: 51
 

Default PHP in temlate

how can I run php in the templates
__________________
x-cart 4.4.3
Reply With Quote
  #2  
Old 09-20-2010, 12:41 PM
 
balinor balinor is offline
 

Veteran
  
Join Date: Oct 2003
Location: Connecticut, USA
Posts: 30,253
 

Default Re: PHP in temlate

{php}

your php here

{/php}
__________________
Padraic Ryan
Ryan Design Studio
Professional E-Commerce Development
Reply With Quote
  #3  
Old 09-20-2010, 02:30 PM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Re: PHP in temlate

Though it's not recommended.
Reply With Quote
  #4  
Old 09-20-2010, 02:47 PM
 
balinor balinor is offline
 

Veteran
  
Join Date: Oct 2003
Location: Connecticut, USA
Posts: 30,253
 

Default Re: PHP in temlate

Right, forgot to mention, best to keep PHP in the php files.
__________________
Padraic Ryan
Ryan Design Studio
Professional E-Commerce Development
Reply With Quote
  #5  
Old 09-22-2010, 05:05 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default 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
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #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

The following user thanks geckoday for this useful post:
minfinger (09-22-2010)
  #7  
Old 09-22-2010, 08:32 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default 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!
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:53 PM.

   

 
X-Cart forums © 2001-2020