Search code examples
viewflightphp

FlightPHP Framework how to return HTML to variable instead of displaying it immediately


It seems FlightPHP framework doesn't work the way I expected, or there is a big misunderstanding from my side. So basically FlightPHP has a method:

Flight::render('myView');

But I cannot figure out or find I a way how do I actually get that view HTML so it would be returned rather than outputting it. I want something like this:

$output = Flight::render('myView');

Apparently that does not work, I also checked FlightPHP framework View class and it seems it has 'fetch' method, that should do just that, but to my surprise calling it does not work, basically such function does not exist, even though it is defined there, I get an error: fetch must be a mapped method. (0)

So maybe someone who has been working with this framework could explain how to solve the issue, or if there's anything else I should be using to return HTML to a variable.


Solution

  • So you could capture it with an output buffer if you wanted to.

    ob_start();
    Flight::render('myView');
    $output = ob_get_clean();
    

    And that'll do what you need. I highly recommend though for anything serious with rendering HTML, I would use an HTML templating library such as Latte

    Additionally, hop in the chat if you have questions you want to ask us directly!