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.
Related
In Smarty: is it possible to call a PHP function (from the controller class) inside the template? there is explained, how to call class methods out of template files.
You nee to assign the object like
$smarty->assign('a', new Controller);
and you can use it like
{$a->foo(5)}
But what, if I want to pass a smarty array value as parameter? It should be something like this:
{foreach from=$dataset item=data}
{$a->foo($data.id)}
{/foreach}
for sure, this won't work. But how can this issue be handled?
Chapter 15. Advanced Features
This is what you need?
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.
I'm hoping someone can point me to the right direction in this.
Lets say I store Smarty template in database. It can look like this:
{currentusername UserID="5"}
The currentusername is a custom function to get the username. All displays correctly when used in a template. But what if I wanted to get the resolved currentusername function in my php code?
Basically I would get the template from the database, resolve it through smarty and then used the currentusername further in the code.
Is that possible?
Edit so it can be better understood. I have a this piece of code:
require('smarty/libs/Smarty.class.php');
$smarty = new Smarty;
$macro = '{currentusername UserID="5"}';
$resolvedmacro = ""; // this should contain the "Jerry";
function smarty_function_currentusername($params, &$smarty){
$UserID = $params["UserID"];
if ($UserID == 5){
return "Jerry";
}
else{
return "I dont know this guy";
}
}
Can I somehow resolve the $macro through smarty so the variable $resolvedmacro would contain the correct value?
Smarty can render a string as though it was a template using the string: or eval: "resource types" - see the documentation on String Template Resources for details.
You can then use $smarty->fetch() to get the output of that "template" into a normal PHP variable:
require('smarty/libs/Smarty.class.php');
$smarty = new Smarty;
$macro = '{currentusername UserID="5"}';
$resolvedmacro = $smarty->fetch('string:' . $macro);
There is a wider question of why you want to do this, and whether Smarty is the right tool for the job - for instance, if there are a limited number of callbacks possible, it might be over-complicating things to use Smarty to resolve them rather than just storing the name of the callback and its arguments and running it through a switch statement.
Note: The string: and eval: resource types were added in Smarty 3; if for some reason you need to use Smarty 2, you will need to write or find a resource plugin to do the same job. You could also write a resource plugin for either version which fetched a particular macro from the database and rendered it in one step; the docs for Smarty 3 are here.
Ok, the subject makes no sense so Ill try to better describe it here.
Zend Frame work in use here. And I have run into a problem passing variables to my views, well the views included into the "top.phtml" that make up the template. What I am trying to do is implement a breadcrumb concept. The bread crumb file is included into the top.phtml before the content view file. So the breadcrumb variable isn't defined as far as the breadcrumb file is concerned.
I can print_r my array of settings for the breadcrumbs within the controllers view, no problem so it is working I know that much, just anything above that view in particular in the order of things can't get the variable. So I guess what I am looking to have answered is is there a means off passing a variable to the overall scheme of things similar in concept to
$this->view->variable_name = blah;
where something as high as the top.phtml can pick it up for use?
You may be looking for Placeholders.
Example:
Setting a placeholder value from a controller:
$this->view->placeholder('some_placeholder_name')->set('blah');
Setting a placeholder value from a view
$this->placeholder('some_placeholder_name')->set('blah');
Retrieving the placeholder value in a view script or layout:
$value = $this->placeholder('some_placeholder_name');
Placeholder content is rendered towards the end of your application execution so the value set in your controller should be available in your top level top.phtml view script.
I think this will work:
$this->layout()->breadcrumbs = ...
And then print $this->layout()->breadcrumbs in your top.phtml.
Zend Layout
After sending hours trying to get placeholder() to work with partialLoop(), I finally gave up and hacked a fix to pass vars to a partial:
$vars = (array) $this->getVars();
foreach ($this->rows as $row) {
$partialVars = array(
'row' => $row,
'vars' => $vars,
);
echo $this->partial('row.phtml', $partialVars);
}
ugly, but it worked.
+1 for everyone for giving me a clue, however none of the above worked well in my favor. However between them all, they lead me towards finding my answer which is
Zend_Registry
In my Controller I built my array and passed it to Zend_Registry like
$breadArray = array(
array("icon"=>"i_robot", "href"=>"systems/"),
array("href"=>"metrics","text"=>"Metrics")
);
Zend_Registry::set('breaded_crumbs', $breadArray);
Then in my breadcrumb.phtml which is loaded before the content and view I used
print_r(Zend_Registry::get('breaded_crumbs'));
to see if it was working, and it gave me the array's so I for anyone in the future looking to extend a variable outside of the view itself, the registry seems to be the way to go. I tried placeholder and layout, both gave me errors about not being something or another, and when I got them to work in part I wasn't getting what I was expecting.
I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and adding their own without modifying the code which i've already written.
How do i make my site pluginable ?
EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.
There may be a better way that's specific to CodeIgniter, but this is what I would do:
First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.
Allow the user to write code like this:
addHook_PreArticle('funcToCall');
function funcToCall( &$articleText ) {
$articleText = str_replace('Hello', 'World', $articleText);
}
addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.
Many CMS's Like Joomla and Blogs like Wordpress use variable function names:
$function="phpinfo";
$function();
You could load this into an array to create a list of functions that can be overridden.
That's a perfect case to use the Observer Pattern.
http://devzone.zend.com/article/5