Skip to content

Instantly share code, notes, and snippets.

@romanodesouza
Last active December 20, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanodesouza/6185176 to your computer and use it in GitHub Desktop.
Save romanodesouza/6185176 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Setting a block that will NOT be overrided -->
{% block someBlock %}{% endblock %}
</head>
<body>
<!-- Using a component from Di -->
{{ myService.doAction() }}
<!-- Setting a block that will be overrided -->
{% block content %}{% endblock %}
</body>
</html>
<?php
use Phalcon\Config;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;
$config = new Config(
array(
'app' => array(
'cacheDir' => __DIR__ . '/Views/cache/',
'compileAlways' => true
)
)
);
$di->setShared('view', function() use ($config) {
$view = new View();
$view->setViewsDir(__DIR__ . '/Views/');
$view->registerEngines(array(
'.volt' => function($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->app->cacheDir,
'compileAlways' => $config->app->compileAlways,
'compiledSeparator' => '_'
));
return $volt;
}
));
return $view;
});
{% extends 'base.volt' %}
<!-- I'm overriding only the 'content' block -->
{% block content %}Phalcon rox \m/{% endblock %}
<?php
// setShared causes WSOD when "compileAlways" is true
$di->setShared('myService', function() {
return new MyService;
});
// "set" works properly when "compileAlways" is true
// EDIT: The example below doesn't work in recent Phalcon versions too
$di->set('myService', function() {
return new MyService;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment