Jump to content
n3m0

Add New Variable In Hook and Show in Template

Recommended Posts

I am complete noob in smarty and have reading tuts, docs provided and googling for whole day (and night), but still have no clue.. so please be patience to me..

I want to show new variable declared in hook php, and show it in certain area at clientareaproductdetails.

I created test.php in hooks folder. The code is:

Quote

<?php
function example() {
    global $smarty;
    $variable = "Hello world!";
    $smarty->assign('VARIABLE_COMES_FROM_HOOK',$variable);
    return $VARIABLE_COMES_FROM_HOOK;
}
add_hook('clientareaproductdetails', 1, 'example');
?>

And  I put the following inside clientareaproductdetails.tpl

Quote

...... many codes above...

{$VARIABLE_COMES_FROM_HOOK}

.... many codes below....

But the "hello world" not show up. Please help what should I do?

Note:
I actually already made many functions in php and want to put inside tpl file, then I just realize whmcs using smarty. And ofc my php functions all failed to load inside tpl file. This is my next plan after I figure it out how "hook" and assign new variable works.

Thank you so much and sorry for my English.
n3m0

Share this post


Link to post
Share on other sites
<?

add_hook('ClientAreaProductDetails', 1, function($vars) {

	$output['something'] = 'Yeah';
	$output['somethingelse'] = 'Nope!';

	return $output;
});

Here is how you use them in tpl file:

<strong>{$something}</strong>
<small>{$somethingelse}</small>

 

Share this post


Link to post
Share on other sites

ah! so simple!.... thank you so much!

Please correct me if I am wrong. if I want to create several PHP functions, I need to create those functions outside of this code bellow :

add_hook('ClientAreaProductDetails', 1, function($vars) {

	$output['something'] = 'Yeah';
	$output['somethingelse'] = 'Nope!';

	return $output;
});

and call them inside it, is that correct?

Or  I need to create those php functionts inside it?

TIA,
n3m0

Share this post


Link to post
Share on other sites

Apologize, I tried exact code from you... create only your code in my hook files, and put the following inside my tpl.

<strong>{$something}</strong>
<small>{$somethingelse}</small>

still don't show anything in my tpl...

Thank you so much for helping me,
n3m0

Share this post


Link to post
Share on other sites
7 hours ago, n3m0 said:

Apologize, I tried exact code from you... create only your code in my hook files, and put the following inside my tpl.

Kian's code would work, except you're using the wrong hook point, it should be ClientAreaPageProductDetails if you're going to return variables to the template...

<?

add_hook('ClientAreaPageProductDetails', 1, function($vars) {

	$something = 'Yeah';
	$somethingelse = 'Nope!';

	return array ("something" => $something, "somethingelse" => $somethingelse);
});

Share this post


Link to post
Share on other sites

I am very sure on the correct place. Because if I tested it by inserting wrong code, it caught the error. But I will try again.
Thank you all for the enlightenment, do really appreciate for your help and time spent on this.

Regards,
N3m0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

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