Search code examples
phpsmartywhmcssmarty3smarty2

How to pass Smarty variable to PHP function?


This is not a duplicate question, I couldn't solve my issue using answers from the other threads, besides my problem is different.

I'm trying to print a URL with an appended parameter using PHP in a Smarty plugin function. The software I use is WHMCS.

Below is a sample code of my function:

<?php 
/* 
 * Smarty plugin 
 * ------------------------------------------------------------- 
 * File:     function.aff.php 
 * Type:     function 
 * Name:     aff 
 * Purpose:  outputs a random magic answer 
 * ------------------------------------------------------------- 
 */ 
function smarty_function_aff($params, &$smarty) 
    { 
           $affiliateid = $params['affiliateid']; 
           $refflink = 'http://example.com/aff.php?aff=' . $affiliateid .''; 

           print $refflink; 
    } 
?>

In the template file, I use {aff} that supposed to print the whole URL including the appended parameter, however it prints the URL like that: http://example.com/aff.php?aff= as if the $affiliateid variable does not exist.

Now if I used the following ($affiliateid} directly in the template file, it will output the user affiliate ID. But I need to use it in the PHP function above. Writing it directly in PHP as $affiliateid doesn't seem to work.

What I'm missing here? Any suggestions please? Thank you.


Solution

  • You can use your function like this:

    {aff affiliateid=$affiliateid}
    

    See this link for more information: http://www.smarty.net/docsv2/en/plugins.functions.tpl

    It states:

    All attributes passed to template functions from the template are contained in the $params as an associative array.

    an attribute (http://www.smarty.net/docsv2/en/language.syntax.attributes.tpl) is the affiliateid=$affiliateid part in my line of code above.