We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get raw HTML before render

Hello,

I want the raw html beforeRender my page, because I want to put a css critical through a node service. I read this: https://docs.phalcon.io/3.4/en/api/phalcon_mvc_view, (I use 3.4) but in my example the print_r is empty... My code is this and my controller name is nombres, action is index and layout is test

public function indexAction()
{
  $view = new \Phalcon\Mvc\View();
  $view->setLayout("test");
  $view->setViewsDir("app/views/");
  // Setting views directory
  $view->start();
  $view->render("nombres","index");
  $view->finish();
  // Printing views output
  print_r($view->getContent());die();
}

Silly layout and action

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>
<?php echo $this->getContent(); ?>
</body>
</html>

<p>This is my action from controller</p>

Do you know why? I saw information in stackoverflow but the same result :( Thx friends!

Can you explain a little more what the reason is for doing this? I've never personally needed to modify rendered HTML after it was generated, so there may be an easier way to do what you're trying to accomplish.



3.1k
edited Dec '19

I mean...before... before render my view I want the raw HTML because I want to put into <head> the critical css and then in the footer put the no critical css link. This is possible because I have a webservice made in nodejs, this service return me a css (into memcache) depending the web page. This work is made in other project that I made with cakephp. I was to manipulate in cakephp a file call view.php, this file give me a raw html before was rendering into browser, so in that moment I can put that css critical and no critical.

And in phalcon i tried to do the same. That's why that code.

Can you explain a little more what the reason is for doing this? I've never personally needed to modify rendered HTML after it was generated, so there may be an easier way to do what you're trying to accomplish.

So this is raw CSS, not a reference to a file? You generate CSS on every page load?

I'd recommend somehow putting this into a static file if possible. Then you can use the AssetManager and Collections to output links to the CSS where necessary: https://docs.phalcon.io/3.4/en/assets



3.1k

Raw CSS is the critical and then CSS no critical is a link. Well I will try to find a solution, I think I will extend the view with the objective to get html or something like that. Thanks for the interest!

edited Jan '20

You can add an if statement in your header tag and output the raw css there.

<header>

...
{% if raw_css is defined %}
<style>
    {{ raw_css }}
</style>
{% endif %}

</header>

You can define raw_css in your controller

$this->view->raw_css = $this->cache->get('raw_csss');

assuming you are keeping it in memcache.

P.S. That is volt syntax in case you wonder.