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

Partial render into a variable

Hello, I'm trying to rewrite existing web with phalcon, with major ups and minor downs so far :) But there is one thing I am not able to do so far, any tries were unsuccessfull. I need in a class (in folder libraries), that gets used in phalcon controller, to use a partial renderer. In basic, im replacing BBCODE tags with their HTML representation and I do not know where in the resulting HTML the partial code will appear, so I need to render the partial into a variable and put this variable into final template. I'd like to see a minimalistic code that is needed to achive that. So far I tried (highly reduced and pseudocoded just to show the main idea) :

#controllers/TopicController.php
class TopicController extends Phalcon\Mvc\Controller
{

    public function indexAction()
    {
         $final_html = BBClass::parse($bbcode_to_be_parsed);
    }
}

#libraries/BBClass.php
class BBClass.php
{
  public function parse($bbcode)
  {
        if (has_tag($bbcode, '[table]'))
        {
        $viewnew = new Phalcon\Mvc\View();
        $viewnew->setViewsDir(__DIR__ . '/../views/');
        $viewnew->start();
        $viewnew->partial("shared/bb/table", array('some_content' => $some_content));
        $viewnew->finish();
        $bbcode_table = $viewnew->getContent();

        //$bbcode_table is empty, no parsing occured :(

        // or, i tried this
        $this->view = $di->get('view');
        echo $this->view->partial("shared/bb/table", array('some_content' => $some_content));
        //only the partial template is shown in the browser, but not the main template and rest of the HTML

        }
  }
}

there are both shared/bb/table.phtml and shared/bb/table.volt templates, in the second case, the volt template is used, as I'm using volt as main template engine in the whole app, but obviously, I'm doing it wrong. I even tried the getRender method mentioned here

https://github.com/phalcon/cphalcon/issues/543

but no luck, I still get either the partial code only (and not the whole page plus partial) or whole page only and empty partial :(

Can you please help me with this ?

thank you !! I had to create new view object and then it worked as expected :

            $viewnew = new Phalcon\Mvc\View();
            $viewnew->setViewsDir(__DIR__ . '/../views/');

            $bbcode_table = $viewnew->getRender('shared/bb', 'table', array('some_content' => $some_content));
edited May '15

Hello, I have a similar problem but I wold like to call also the action that prepares all parameters for rendering the view. For example in the page Using Views this example is given:

<?php
$view = new \Phalcon\Mvc\View();

//A trailing directory separator is required
$view->setViewsDir("../app/views/");

// Passing variables to the views, these will be created as local variables
$view->setVar("someProducts", $products);
$view->setVar("someFeatureEnabled", true);

//Start the output buffering
$view->start();

//Render all the view hierarchy related to the view products/list.phtml
$view->render("products", "list");

//Finish the output buffering
$view->finish();

echo $view->getContent();

but instead of

$view->setVar("someProducts", $products);
$view->setVar("someFeatureEnabled", true);

I want

$controller = new Products();
$controller->list();

What is the best way to achieve this ? Many thanks.



696

Please, reformat your post se code blocks display correctly, put a double line jump before opening php tag and close it in a new line (after the code you want to paste) and then write the the text in a new line (another line jump), so:

  1. text... (your cursor is here)

  2. Line jump

  3. Line jump

  4. Open php tag as you did in your post

  5. Line jump

  6. Write php code

  7. Line jump

  8. Line jump

Maybe the answer to your question is here, take a look: https://docs.phalcon.io/es/latest/reference/dispatching.html

you can render a partial into a variable with $view->getPartial(<partial_path>); in controller (most likely needed in ajax) you can write $this->view->getPartial(<partial_path>);

This is my solution.

ob_start();
foreach ($data['accounts'] as $account) {
    $this->view->partial('widget/each-account-stats-row', [
        'account' => $account,
        'otherParameter'  => $otherValue,
    ]);
}
$myVariable = ob_get_clean(); // it gets all output (rendered partials) from ob_start()