How to compile a Volt string with variables for substitution like in a view (but not using views)?

I want to compile a volt string and to be able to pass in values like how a view would do it and withoout writing it to disk. I'm trying to use Volt to replace some home grown parsing code.

Also is there a way to disable pretty much all of the Volt functions, especially the include and partial stuff?



982

I don't think you can do this with phalcon. I ended up using twig after a whole day of trying to accomplish exactly what you're trying to do. Even the twig docs say, "Don't ever use this function" with regard to the Twig\Loader\String function, so I don't think that phalcon was designed with string parsing in mind.



41.7k

Well how are the Views doing variable substition if its not possible with Volt? How does {{ varAddedToView }} get substitued in a Volt template if there is no way to do it?



982
edited Feb '15

Pardon me, when you said volt string, I assumed that you meant that you were trying to use volt to parse a template that existed in a string variable. To pass variables to volt in order to parse them as tags in a .volt file. In order to render volt out to a string, and pass an array of variables in, you will need to create a new instance of the View.

$view = new Phalcon\Mvc\View();
//Set this up exactly like you would set up the view in your dependency injector, except it is only going to exist in the scope of the parsing action.
//Then you'd simply tell it what to point to:
$view->setViewsDir('path/to/my/views');
//Pass in your variables.
$params = [
    'varAddedToView' => 'fred'
];
$outputString = $view->getRender('controller','action', $params);
return $outputString;

I can't test that, but the code should be close. You'll have to set up some fake paths to get the controller and action view bits to work.



41.7k

Pardon me, when you said volt string, I assumed that you meant that you were trying to use volt to parse a template that existed in a string variable.

You understood me correctly the first time. I already have a full functional system for using views and simple views. Now I want to use Volt in other use cases such as compiling a string out of of a database. I just want to use certain features of Volt without compiling PHP. Perhaps Volt is just not designed to do that.

I'll probably end up reusing by custom parsing code.



982
edited Feb '15

Yes, that's what I was saying, and that's why I used twig instead. Twig was easy to implement using composer and it had a function that did exactly what I needed. Volt simple wasn't designed with parsing string variables in mind.

Edit: I actually figured out an extremely hacky what to do what you want, but I'd advise strongly against using it.

$view = new View();
    $viewvars = [
        'greeting' => 'Hello',
        'name' => 'Fred'
    ];
    $volt = new View\Engine\Volt($view, $this->getDI());
    $template = "{{greeting}}! My name is {{name}}.";
    $string = $volt->getCompiler()->compileString($template);
    $filename = md5(microtime(true));
    $tmpPath = '/tmp/php_template_cache/' . $filename . '_temp.php';
    file_put_contents($tmpPath, $string);
    extract($viewvars);
    include($tmpPath);