Jump to content

Add New Variable In Hook and Show in Template


n3m0

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

Link to comment
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>

 

Link to comment
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

Link to comment
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

Link to comment
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);
});
Link to comment
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

Link to comment
Share on other sites

  • 1 year later...

This def does not work, it adds the data to `addons_html` for some reason (at least when I use `ClientAreaHomepage`).  For anybody trying to do something like this, you can always check this by adding `{debug}` to your smarty template and it will popup all the variables.

The easiest solution for this is to just add the variables to the global smarty variable.  This will work anywhere in code or hooks, as smarty variables (as of 8.1 are still stored in the global `$smartyvalues` parameter)

 

add_hook( 'ClientAreaHomepage', 2, function( $params ) {
	global $smartyvalues;
	$smartyvalues['testME'] = 'YUP YUP';
});

 

Link to comment
Share on other sites

6 minutes ago, HostT said:

This def does not work, it adds the data to `addons_html` for some reason (at least when I use `ClientAreaHomepage`). 

ClientAreaHomepage returns HTML rather than an array... ClientAreaPageHome would return an array/variables to the template.

Link to comment
Share on other sites

  • 3 years later...

Use 'ClientAreaPage'  to execute across the entire client area pages, Products, Templates, Carts, etc.

 

Saves me time!

 

<?
 
add_hook('ClientAreaPage', 1, function($vars) {
 
    $output['something'] = 'Yeah';
    $output['somethingelse'] = 'Nope!';
 
    return $output;
});
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