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.
Related
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've connected Smarty to CodeIgniter and now of course i cant use {memory_used} and {elapsed_time}, but i want to know how i can output this information to my webpage now?
Benchmark class
$this->benchmark->elapsed_time()
$this->benchmark->memory_usage()
They do return their mustached values that are parsed by output class later, so if you use Smarty, you can basically use the raw functions CodeIgniter uses itself.
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
global $BM; // needed only for elapsed_time
$elapsed_time = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
Note it is really a workaround and there should be better ways to do it.
Just read the documentation. Its right there.
$this->benchmark->elapsed_time() and $this->benchmark->memory_usage()
I couldnt do "global $BM" inside a smarty template, so in my controller constructor I did:
global $BM;
$this->smarty->assign("BM", $BM);
and then in my template:
{$BM->elapsed_time()}
{literal}{elapsed_time}{/literal}
Tested and works.
I'm using a really basic library in Codeigniter. In order to use it I need to pass in a number of config parameters using a config function. My library currently requires me to instantiate it before I can call the config, i.e., I have to use it as below:
$this->load->library('Library');
$instance = new Library();
$instance->config($configparams);
I'd like to use it like standard CodeIgniter libraries:
$this->load->library('Library');
$this->library->config($configparams);
What do I need to add to the library in order to have it auto-instantiate? The code is as below:
class Library {
function config($configparams){
...
}
}
This is working now. I swear it wasn't working before I posted on SO! Thanks for posts.
Once you load a class
$this->load->library('someclass');
Then when use it, need to use lower case, like this:
$this->someclass->some_function();
Object instances will always be lower case
According to the docs, you should just call it. So:
$this->load->library('Library');
$this->library->config($configparams);
But why not just pass $configparams to the constructor:
$this->load->library('Library', $configparams);
Check out the guide for CodeIgniter -- it's a great resource to learn more about the framework. IMHO, there aren't any good books available on the current version; this is it.
You basically call it like anything else.
$this->load->library('Name of Library')
Read more here: http://www.google.com/url?sa=t&source=web&cd=2&ved=0CCIQFjAB&url=http%3A%2F%2Fcodeigniter.com%2Fuser_guide%2Fgeneral%2Fcreating_libraries.html&ei=tLFUTbz3HI3SsAOYgP2aBg&usg=AFQjCNFo751PYFp5SbqzuZMxGhXwMI8SJA
Is it possible to call a php class function DIRECTLY using ajax?
Something like below... except ajax...
myclass::myfunction();
I've been using the jquery library to work with AJAX.
$.get('control.php', {func: funcName, arg1: arg1});
The above is similar to what I'm trying to achieve MINUS the control.php;
I'm not sure if this is even possible, but I just thought it would be nice to skip the landing page (control.php) that recieves the funcName. I have a bunch of conditional statements that sort out what class function to run based on the funcName recieved.
It seems kind of silly to do this, to have a separate page just to handle function calls.
Is there a better way?
No.
If this were possible, it would be a gaping security hole.
No. You can't invoke a method directly that way.
You could use routing (like the technique used in CodeIgniter and CakePHP) but that's just syntactic sugar that does the same thing -- control your routes to actions.
It is not possible because of a simple reason. How should the AJAX knows, where to find the function. It needs to have a URL to locate the function so it doesn't work without a php file in between.
No for security reasons but there is no reason why you can't do something like this
function run($args){
//do stuff
}
echo run($_REQUEST);
//or
echo run($REQUEST['name']);
I'm using GWT to dynamically load html snippets from php script. I define the snippet i want the php script to return in the url (test.php?snippet=1). Now in GWT i have a function "getSnippet(int snippet id)" that uses a RequestBuilder to retrieve the snippet. It works perfectly fine, but it bothers me that i have to create a new RequestBuilder everytime getSnippet gets called. I'd rather have one ReqestBuilder and just change the url when getSnippet is called...
Is there a way to do this ?
Thank you !
In looking at the source code, I can't see a good reason why they are doing this. I would like to think that the GWT developers decided to leave out the setUrl method for a reason and included it in the constructor instead.
If you really want to do it, one way around this would be to extend the class and add a setUrl(String url) method. Modify all your current uses of RequestBuilder to use your newly extended class and see if anything breaks.