Jump to content

How to pass Smarty variable to PHP function?


Michael F

Recommended Posts

Greetings, I'm writing a custom PHP function in the Smarty plugins directory. The problem is that I need to use the $affiliateid variable but it seems that it does not work at all, and not recognized by the function.

 

Does anyone have a clue on how to use or pass a Smarty variable in PHP?

 

Thank you :)

Link to comment
Share on other sites

Update:

 

A sample code of what I'm trying to achieve:

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     function.*
* Type:     function
* Name:     aff
* Purpose:  outputs a random magic answer
* -------------------------------------------------------------
*/
function smarty_function_aff($params, &$smarty)
{
       //none of the following display the affiliate id when called in template.
	print $affiliateid;
	echo $affiliateid;
}
?>

 

And in the template I use {aff} that supposed to display the affiliate ID of the user, however it doesn't display anything, just a blank white area. So my guess is that the PHP function does not recognize the $affiliateid variable.

 

I know I can use {$affiliateid} in the template itself to display the user affiliate ID but I have a different situation. The above code is just a sample.

 

Any suggestions?

 

Thank you in advance :)

Link to comment
Share on other sites

what kind of plugin your working on modifier, Prefilter, etc?

 

To be honest, I don't have much experience in PHP, so I don't know actually. It's pretty much like the function code I added in my previous reply but with replacing the core code. I need to call that function in the affiliates.tpl file like that {aff}.

 

I'm wondering why is the function not able to read the $affiliateid variable. I'd greatly appreciate it if you could shed some light on this. Thanks :)

Link to comment
Share on other sites

if you are writing a smarty function, this is how to pass the variable to it:

 

{functionName affiliateid=$affiliateid}

 

then from inside the function it self you can read it like that:

function smarty_function_functionName($params, Smarty_Internal_Template $template)
{
   $affiliateid = $params['affiliateid'];
}

Link to comment
Share on other sites

if you are writing a smarty function, this is how to pass the variable to it:

 

{functionName affiliateid=$affiliateid}

 

then from inside the function it self you can read it like that:

function smarty_function_functionName($params, Smarty_Internal_Template $template)
{
   $affiliateid = $params['affiliateid'];
}

 

Thank you for the answer, it is greatly appreciated :)

 

I'm trying to append the affiliate ID to URL. I modified my code to contain what you've mentioned but I couldn't make it work.

 

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

          print $refflink;
}
?>

 

Still the $affiliateid doesn't seem to work as if it is not even exists. It prints only the first part of the URL without the affiliate ID, like this: http://example.com/*?aff=

What I'm missing here?

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