call function inside template in fatfree

726 views
Skip to first unread message

FreeFAT

unread,
Jun 19, 2017, 10:51:52 AM6/19/17
to Fat-Free Framework
Hi everyone,
I use fatfree 3.6


I have som class:

class Some {
function runMe{
return "YOU DID IT!";
}
}

and in my template (which calling like:  echo Template::instance()->render('app/theme/main.html'); )

I want to run runMe() function, call this function by Some->runMe()

when I call {{Some->runMe()}} it shows Fatal error ... WHY?

What to do to call any functions in template without $f3->set('myclass', new Some);




FreeFAT

unread,
Jun 19, 2017, 11:04:35 AM6/19/17
to Fat-Free Framework
Help please,

who knows how to call Some->runMe() function inside of template in fatfree.

Thanks.

FreeFAT

unread,
Jun 19, 2017, 11:13:30 AM6/19/17
to Fat-Free Framework
I get fatal error

syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ';' 

ikkez

unread,
Jun 19, 2017, 11:54:18 AM6/19/17
to Fat-Free Framework

"Some" is not initialized within template. Try to extend Prefab instead:

class Some extends \Prefab {

 
function runMe{
   
return "YOU DID IT!";
 
}
}

and then in template:

{{ Some::instance()->runMe() }}

FreeFAT

unread,
Jun 19, 2017, 12:26:00 PM6/19/17
to Fat-Free Framework
Thanks, very good idea.

BUT ...

what if my Some class already extends Controller?

what to do then?

class Some extends Controller{

  
function runMe{
    
return "YOU DID IT!";
  
}
}

then it shows an error ... instance unknown method.


понедельник, 19 июня 2017 г., 19:54:18 UTC+4 пользователь ikkez написал:

FreeFAT

unread,
Jun 19, 2017, 1:31:17 PM6/19/17
to Fat-Free Framework
will be enough use this code?

class Controller extends \Prefab{

....

}

Then all classes has 'extends Controller' will be initialized within template. Am I right?



понедельник, 19 июня 2017 г., 20:26:00 UTC+4 пользователь FreeFAT написал:

Nuwanda

unread,
Jun 21, 2017, 9:23:02 PM6/21/17
to Fat-Free Framework
Hang on, can't you simply assign an object method to a framework variable with $F3->set and call that in your template?

Summer White

unread,
Jul 6, 2017, 6:08:05 AM7/6/17
to f3-fra...@googlegroups.com
When I'm debugging templating issues like this I check the compiled version of the template in the tmp folder.

This way you can just see what php fat free is actually generating. This way you can figure it out in the context of php instead of fat free.

Sometimes I do this just to see what features I can push out of fat free =P

FreeFAT

unread,
Jul 6, 2017, 7:20:29 AM7/6/17
to Fat-Free Framework
Don't you understand that I have a great class in php ... I just showed an idea and problem.
if I do SET in fatfree all my need variables then there will be a mess. 
I do need call methods without $F3->set.
Anyway thanks.

четверг, 22 июня 2017 г., 5:23:02 UTC+4 пользователь Nuwanda написал:

xfra35

unread,
Jul 6, 2017, 8:46:57 AM7/6/17
to Fat-Free Framework
if I do SET in fatfree all my need variables then there will be a mess

You don't get it. What Nuwanda suggested was to bind one variable to your controller method:

some.php

function Some extends Controller {

 
function get($f3) {
    $f3
->set('runMe',function(){
     
return $this->runMe();
   
});

    echo
Template::instance()->render('app/theme/main.html');

 
}


 
protected function runMe() {
   
return 'You can access anything: '.print_r(get_object_vars($this),true);
 
}


}

main.html

{{ @runMe() }}


We can't judge if it's a good idea or if it fits your needs, but hey... it works!
Reply all
Reply to author
Forward
0 new messages