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

Render volt syntax in a string

Hi guys,

I have a question for you, is it possible to render volt syntax from a string and return the content?

here my case:

I'm implementing a widget system like this one https://forum.phalcon.io/discussion/330/widgets-with-volt-rendering

and is working fine in .volt files but I want to be able to render that syntax form a varible, basically I want to store the page content in the database like

<h1>Content</h1>
{{ render_widget('MyWidget', [$param1, $param2]) }}

store that string in the database and when the controller is called render the syntax and pass it to the view like this:

$page = new Page();

$content = $page->content;
//compile the content
$out = compiledContent;

$this->view->setVar("content", $out);

and the HTML will be

<h1>Content</h1>
<div>I'm MyWidget widget!</div>

is that possible?

While I'm talking about pages I have another question, is possible to call a function from the index controller without specify the index controller?

eg:

https://www.mysite.com/about

is calling the about controller, but I want to render the about page stored in the database, any thought?

edited Jun '14

Look here for full example how to compile string: https://forum.phalcon.io/discussion/2434/adding-events-before-and-after-volt-compilation-#C8261

Shortly:

$compiler = $volt->getCompiler();
$result = $compiler->compileString($yourVoltString);

thanks for your answer, it work, but I have to create another instace of the volt engine, is there a way to get the current compile inside the controller?

any Idea about my second question?

thanks mate!

edited Oct '14

I played around with the compile but I think I'm missing somethig, here what I want to accomplish:

An arbitrary string with volt code inside:

$string = "Hello User, {{dashboard()}}";

so if that specific string is inside a volt template the {{ dashboard() }} is translated in <?php echo Helpers::dashboard() ?> and is working just fine,

but I would like to compile that volt string, execute the function and return the content, something like

controller

//compile and execute the volt string and assing the result to the $result variable
$this->view->setVar("output", $result);

I'm pretty sure that thi behavior can be achieved, any idea how to do that?

Thanks

Did you managed to do it?

I was able to write the "compiled" view to a tmp folder and then render it. I'm searching for a $string->compile->render(with variables) on the fly.