WHMCS, how to create template variables from my hook - whmcs

My question maybe is simple, but anfortunatly I don't know how to answer it.
How can I create a variable in a hook function and then pass a PHP variable from within the function to my Template in order to use it in the form of {VARIABLE_COMES_FROM_HOOK}

WHMCS uses Smarty template engine so as an example inside the includes/hooks folder you will have your hook file e.g hook_example.php from which you can assign variables to $smarty global object and access them in your template as {$VARIABLE_COMES_FROM_HOOK}.
Inside hook_example.php:
function example() {
global $smarty;
$variable = "Hello world!";
$smarty->assign('VARIABLE_COMES_FROM_HOOK', $variable);
}

Search in hook categories http://docs.whmcs.com/Hooks
There are hooks that return variables to tpl and other that don't do that

Related

What globals are available in template expressions?

I'm reading the Vue.js guide, and I've come across the statement.
Template expressions are sandboxed and only have access to a whitelist
of globals such as Math and Date. You should not attempt to access
user defined globals in template expressions.
What are all the globals available in templates?
That is, what is the content of the whitelist, exhaustively?
Vue.js defines the whitelist of globals in the core/instance/proxy.js file:
//...
const allowedGlobals = makeMap(
'Infinity,undefined,NaN,isFinite,isNaN,' +
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
'require' // for Webpack/Browserify
)
// ...
When Vue compiles the template, the string interpolations are processed, and if you reference a global that is not whitelisted, you will have a warning on Development.
If you are curious about how templates are compiled to render functions, look through the template explorer.
From what I understood from source code globals are declared in a variable and make it available via a proxy between vm instance and template :
const allowedGlobals = makeMap(
'Infinity,undefined,NaN,isFinite,isNaN,' +
'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
'require' // for Webpack/Browserify
)
So gobals available in template are :
Infinity
undefined
NaN
isFinite
isNaN
parseFloat
parseInt
decodeURI
decodeURIComponent
encodeURI
encodeURIComponent
Math
Number
Date
Array
Object
Boolean
String
RegExp
Map
Set
JSON
Intl
require
If you try to put in your template a reference that is not whitelisted or not in your vm instance then you will have this warning :
Property or method "${key}" is not defined on the instance but
referenced during render. Make sure that this property is reactive
either in the data option, or for class-based components, by
initializing the property.

Cant access helper methods inside mongoose schema

My directory looks like bellow
--controllers
-helper.js
--models
-userModel.js
--server.js
My helper module is like
module.exports = {
check: function() {
return 'check';
}
}
I want to access helper module inside userModel.js. So I put like
var helper = require('.././controllers/helper');
Then I do console.log(helper.check()); but it shows error helper.check is not a function Or if I do console.log(helper); only it returns {}. How to access the helper module inside models? Thank you.
Since you said it returns {}, can you please check in your helper module that you have imported userModel.js. Because it forms circular dependencies and sometimes result empty json.

Global variables objects React-Native

I have one class ContentManager on android objects and global variables ContentManager style, the aim is to create this object from one time only, you can get set properties for it anywhere in the app, now declared on React native the declaration is this? the picture below is me java code on android. Help!!!!!!!
code in java
code in java
create global.js
module.exports = {
// your global variable
},
};
Then require it at the top in your file
GLOBAL = require('../global');
for access
GLOBAL.your_variable_name

Where define new global variable in Prestashop

The file defines.inc.php contains multiple globals variables but if I want to define new variable which file is the best ?
If I update Prestashop the file defines.inc.php is reset and I loose my global variable.
Maybe in settings.inc.php but this file is not versioned.
You can create a file config/defines_custom.inc.php next to config/defines.inc.php. At startup Prestashop checks if this file exists. If it exists then it is included before the default one.
You can find the related code in config/config.inc.php :
$currentDir = dirname(__FILE__);
/* Custom defines made by users */
if (is_file($currentDir.'/defines_custom.inc.php')) {
include_once($currentDir.'/defines_custom.inc.php');
}
require_once($currentDir.'/defines.inc.php');
This way you can for example set mode dev on without touching the default file:
define('_PS_MODE_DEV_', true);
And in the default file, this define will not occur:
if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', false);
}
I suggest to create your own module (maybe a 'dummy' module :)), and declare there your global variables.
For example create a module called 'mymodule', the main file mymodule.php should be:
// Here you can define your global vars
define('MY_CUSTOM_VAR', 100);
class MyModule extends MyModule
{
public function __construct()
{
// See documentation
}
public function install(){ return parent::install(); }
}
So you can update your PrestaShop version without problems losing your global vars ;)

Custom Cookie variable + Prestashop

Prestashop
I am stuck we one problem for cookie. In prestashop 1.4.7 I create a custom cookie variable using setcookie but when i am try to access and assign it on front-controller, I am not getting cookies set value.
here is my script:
page: checkpostcode.php
include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
global $cookie;
setcookie("is_postcode_checked", 1, time()+600, "/", "", 1); // Set the cookie in basepath
On frontcontroller.php page :
I am access it using $_COOKIE and assign it into smarty array.
'is_postcode_checked' => $_COOKIE['is_postcode_checked'] // Getting null value for cookie
page: checkpostcode.tpl
{$cookie->_get(postcode_checked_msg)} // here get the is_postcode_checked value but
but I am not able to get is_postcode_checked variable value.
In prestashop 1.5, global are deprecated.
To set something in the cookie :
In a controller :
$this->context->cookie->__set($key,$value);
Other file :
$context = Context::getContext();
$context->cookie->__set($finger_print,$result);
You can access to your value with :
In a controller
$this->context->cookie->key
Other file :
$context = Context::getContext();
$context->cookie->key;
You should use Prestashop's own cookie class entirely rather than using the PHP setcookie() function. The class uses the "magic methods" __get(), __set(), __unset() and __isset() which should allow you to do this easily.
Try in your "page" code (not sure how you're executing this since it doesn;t look like an additional page controller):
global $cookie;
$cookie->is_postcode_checked = 1;
$cookie->write(); // I think you'll need this as it doesn't automatically save
...
And in your FrontController override:
global $cookie;
if (isset($cookie->is_postcode_checked))
$is_postcode_checked = $cookie->is_postcode_checked;
else
$is_postcode_checked = 0;
You can assign the variable $is_postcode_checked to a corresponding smarty variable to use in your template.
If you want to fetch the cookie from Prestashop cookie class, you should store it in this class too.
Use die() function in your controller, to find out is that cookie set
It is better, as Paul said, to use only global $cookie class to store and getting data.