Pass several variables from Smarty to PHP function - php

We got a shopsystem working with Smarty. I need to pass some Smarty variables to a PHP function and get the return.
What I know and also did so far is the following:
{$order_id|#get_order_total}
So this passes the Smarty Variable "$order_id" to a included PHP file which contains the function get_order_total($order_id) and shows me the return of this function.
Now I need to pass 3 variables to a PHP function. The function would for example look like this:
handleDebit($order, $payCode, $insertId)
Sadly i have not found the right thing so far in smarty documentation. Anyone has ever done this?

If you really need to call the function from within smarty templates, register a wrapper function as smarty-plugin:
<?php
$smarty->registerPlugin("function","handleDebit", "handleDebitSmarty");
function handleDebitSmarty($params, $smarty)
{
return handleDebit($params['order'], $params['payCode'], $params['insertId']);
}
Now you can use it as smarty tag:
{handleDebit order=$blah payCode=$blub insertId=$yeahh}
But you should consider #JonSterling s advice and try to find a way auch that a controller is doing the handleDebit-call and you only handle results/display-stuff in the template.

Related

Rendering data in Laravel 5.1 using shortcodes like wp

I have the below content in my DB-
<p>This is dummy content for testing</p>
{{LandingPageController::getTest()}}
I want to render that into my view. But when I'm rendering this in Laravel view, this {{LandingPageController::getTest()}} is getting displayed as it is stored in DB.
I want to call the LandingPageController getTest method in my view.
Please suggest me a quick fix for this.
Landing Page Controller
public function getTest(){
return "Hello World!!!";
}
just make the function static
public static function getTest(){
return "Hello World!!!";
}
that's the only way you can call it like this {{LandingPageController::getTest()}} but I do advice not to do that in your blade file this not a good code design. you should do $test = LandingPageController::getTest() in the controller that you return the blade view and pass it like this return view('blade_file_name',compact('test')) and in your blade file just do {{$test}}
PS - if you doing it your controller use the class like this use Path\To\Controller\LandingPageController
Use namespace for that controller in your blade file. example
namespace App\Http\Controllers\LandingPageController;
You can evaluate a string as a php code using the eval() function
eval — Evaluate a string as PHP code
But it is highly discouraged.
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
You can use a generic string, {test} for example, when saving the content in the storage.
<p>This is dummy content for testing</p>
{test}
Then whenever you need to display the actual content, you can simply replace the generic string with the real value. You'll have this line in your blade file:
{{ str_replace('{str}', "Hello World", $content) }}
Take a look at Helper. You can call helper function in view to render your text or html
Got the solution, achieve the functionality with "laravel-shortcodes".
Found a very good tutorial on laravel-shortcodes like wordpress

Working with smarty template engine for WHMCS. Need to use php function from external php file in .tpl file

Trying to fetch output in A.tpl but not getting any output. I think i'm doing something wrong to call php function in tpl file.
A.tpl
{myModifier}
B.php
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template)
{
return "100.70";
}
}
$smarty1 = new Smarty();
$smarty1->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
I 've already used this code. And this doesn't seem to work. It also breaks my existing working code in A.tpl post this use.
My Need here is to get output from the php function in A.tpl from an external php file.
Thanks in Advance. Sorry for being noob.
To add this modifier to Smarty and use it in your template, you're best to use a WHMCS hook.
If you create a new PHP file under the '~/includes/hooks/' directory (you can name it anything - in this case, let's use 'myhook.php'), WHMCS will automatically inject this hook on each request.
For this, you're going to want to use the ClientAreaPage hook. Inside your hook, you can then access the global $smarty variable.
Example:
function MySmartyModifierHook(array $vars) {
global $smarty;
// I recommend putting your Geolocation class in a separate PHP file,
// and using 'include()' here instead.
class Geolocation{
public function sm_loc($params, Smarty_Internal_Template $template) {
return "100.70";
}
}
// Register the Smarty plugin
$smarty->registerPlugin('modifier', 'myModifier', array('Geolocation', 'sm_loc'));
}
// Assign the hook
add_hook('ClientAreaPage', 1, 'MySmartyModifierHook');
That should do the trick. If you want to explore with other hooks, you can take a look at the Hook Index in the WHMCS documentation.
The function names in each of your hook files must be unique.
As a side note, if you're only wanting to run this hook on specific pages, you can check the templatefile key in the passed $vars array. For example, say you only wanted this hook to run on the 'View Cart' page on the order form:
function MySmartyModifierHook(array $vars) {
global $smarty;
// If the current template is not 'viewcart', then return
if ($vars['templatefile'] != 'viewcart')
return;
// ... your code here ...
}
Also, note that with hooks like the 'ClientAreaPage' hook, returning an array of keys and values will automatically add them as Smarty variables. So if your hook function ended with return ['currentTime' => time()];, you could then use {$currentTime} in your Smarty template to output its value.

Changing a Smarty value from a plugin function?

I have a plugin function which should be used to modify a given Smarty variable which is an array.
After reading the docs, it looks like this should be the way to do it:
$var = &$smarty->getTemplateVars($params['var']);
$var['blah'] = 'aaa';
... but it doesn't work. The array, as seen by other template code after the call to this plugin function, sees the array unmodified.
So, how can a plugin function modify a template variable?
Unless someone figures out a solution, it looks like it cannot be done in a "function" plugin type. It can be done in a "modifier", though.

Execute Twig function in PHP

Is it possible to execute a Twig function in PHP ? For example, the twig function path() is used in our project to generate the URL of a page, I would like to be able to access this in PHP as well. Is this possible ?
No. I'd recommend to go the other way around and rewrite the function in php. Then use them also in twig with functions or filters.

Trying to call a smarty plugin (with params) in smarty foreach

I'm trying to embed a personal plugin into my smarty TPL files, but I couldn't make it work...
This is my smarty plugin:
<?php
function smarty_function_alticerik($params, &$smarty) {
if (!function_exists('alticerik')) {
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
}
return $CI->IceriklerMod->liste($params['where']);
}
?>
And these are my jabber wocky TPL codes:
{foreach item=alt from=alticerik|#alticerik(where="where ustid=$ustid")}
{$alt.id}
{/foreach}
I have searched and read all of the smarty help pages but I still have no idea that how can I make this codes work correctly...
I believe your issue is that functions don't get called using that Smarty syntax.
What you're doing is sort of a mix between a function and a modifier.
Modifiers change a given input - for example, lower casing a string.
{$upperCaseWord|strtolower}
Functions take named parameters and usually do a bit more work such as creating a dropdown.
{html_options options=$arrayOfOptions selected=$selectedValue}
In your case, I'm guessing that you want to use a modifier since you look to be attempting to modify a value. You can still pass options into those, but they aren't named and it gets fairly confusing quickly. However, the code might be:
{foreach item=alt from=$alticerik|#alticerik:"where ustid=$ustid"}
{$alt.id}
{/foreach}
Meanwhile your actual function looks like:
<?php
function smarty_modifier_alticerik($input, $whereString) {
// Here, $input is the same as your $alticerik variable
// and $whereString is just the string that comes after the colon.
if (!function_exists('alticerik')) {
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
}
return $CI->IceriklerMod->liste($whereString);
}
?>
Note, however, that in your code, you don't end up using the value of $alticerik from your template, so it makes me wonder if you need a function instead. I can't really know for sure unless I get what the alticerik plugin is supposed to do.
For more information on functions and modifiers, see the documentation here: Smarty.net Function Documentation and here: Smarty.net Modifier Documentation.
Do you mean $CI =& get_instance(); perhaps?
and what's not working? Any errors?

Categories