Jump to content

include php file in tpl file


WHMCS Andrew

Recommended Posts

  • WHMCS Developer

Hey,

 

I am trying to include a file in my tpl file and pass a smarty variable in to it.

--

What I am trying to do, is get the monetary amount from the fields displayed and show a conversion to USD based on a rate set in the database.

--

This is what I have so far.

 

 
{assign var='t1' value=$total|replace:$currency:''} 
//This replaces the currency words with nothing
{assign var='t2' value=$t1|replace:$currencysymbol:''} 
// this replaces the symbol with nothing

//Now the hard part:
{include_php file="/home/xxxxx/public_html/billing/includes/conv.php"} 
//<-- This works but does not calculate anything as I am not passing anything through

{include_php file="/home/xxxxx/public_html/billing/includes/conv.php?amount=`$t2`"}  
\\<== This doesn't work. 







 

Anyone any ideas how I can sort this? Not really used to the smarty templating system yet. (And I have tried the smarty manual which doesn't really help a newbie to smarty)

Link to comment
Share on other sites

You could also check out http://www.phpfreaks.com/smarty_manual/page/language.function.include.php.html

{include_php} tags are used to include a php script in your template. If security is enabled, then the php script must be located in the $trusted_dir path. The {include_php} tag must have the attribute "file", which contains the path to the included php file, either relative to $trusted_dir, or an absolute path.

By default, php files are only included once even if called multiple times in the template. You can specify that it should be included every time with the once attribute. Setting once to false will include the php script each time it is included in the template.

You can optionally pass the assign attribute, which will specify a template variable name that the output of {include_php} will be assigned to instead of displayed.

The smarty object is available as $this within the PHP script that you include.

which means that the variable would not need to be passed as a $_GET but as $this->t2 ... I'm assuming you can edit the conv.php file?

Link to comment
Share on other sites

  • WHMCS Developer

Hi All,

 

the phpfreaks bit is the same manual as the smarty one.

 

I'm really lost now.

 

Trying everything listed in the manual.. and it's not working..

 

OK, file: function.conv.php put in the "/whmcs/libs/" directory.

 

I'm not too sure on the DB stuff, so if anyone can lend a helping hand :)

<?php
// setup our function for fetching stock data
function fetch_amount($amount)
{
 // put logic here that fetches $ticker_info
 // from some ticker resource
 $db = new Db();
$db->query('select Rate from tblCurrency where Currency = 'USD' LIMIT 1');
  $ticker_info = $db->getRows();
  $ticker_info *= $amount;
  return $ticker_info;
}
function smarty_function_conv($params, &$smarty)
{
  // call the function

  $ticker_info = fetch_amount($params['amount']);
  // assign template variable
  $smarty->assign($params['assign'], $ticker_info);
}

 

then in the template (reading the manual.. I'm assuming this should work)

 

{conv amount=`$t2` assign="convert"}

 

I have tried the $t2 with and without the ` marks and the page just comes up white.

 

Can anyone suggest any code changes for my function file?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated