ZF2 + Blitz templates
Hello, razlyubezny my habrovany. And if one of you web development in PHP is involved, then all are welcome. I've just now started to look at different frameworks... Divorced they have Pehape passion. Apple nowhere to fall, as they say blondes.
I remembered then that a long time ago (when the DOM tree was large, and Unicode single-byte) ashamed to have made a tiny commit the Zend Framework (still only 1 version was). Let me think, see — what lives, what breathes this Zend today. Look — up-how... grown up... And I remember it so high... Began to read, what will happen, and even article about the PSR-7 HTTP Messages and their implementation in PHP. Oh, my goodness! We must continue to look, we have to learn, learn and learn. And then come the young punks, so how can you wipe me off the face of the Earth:
All had to learn all over again. I grunted, taken Getting StartedQuest Guide, I thought. Something in all this I did not like. Can, dark words "Skeleton Application" in the upper part? Or what config for Nginx I had to write (which they gave me their Apacia a mug in the face poke?). No, not really...
Oh! In ZF2 there is in fact a template engine in PHP!
Over the years, Badoo templates Blitz became familiar and family. We need them here urgently screwed. Googled — no. What the hell?? (maybe I haven't googled it?) In General, cleanscalpel XDebug, took the cotton wool, alcohol and cucumber, and began to dissect.
The patient was made by application PDF. More precisely, fork.
Tek-C, view... What is a healthy liver in a patient, don't you think, colleague? Even in the coffin put... that is what we transplant. I am under the liver entail RendererStrategy of the Zend View Quick Start. Take a class AcceptStrategy and copied to yourself. Let's call the copy, as usual, Polwart. Just kidding, we will call Application\View\RenderingStrategy. Why not BlitzRenderingStrategy? Yes, sobsno, it creates and configures our rendering engine, which means that it will be the same, for example, to customize the rendering JSON in the case of the relevant request. So here it is.
The resulting class I amputated properties "xxxRenderer" — and why do we need immediately? Well, to have on hand sendowski ServiceManager, decided to pass it in the constructor:
selectRenderer() and injectResponse() had to rewrite. That's what you get!
While the images were not.
Here. Already the whole class did! But it was not enough! It is necessary that our "module" (a word what — moodul) were aware of the fact that we are preparing! What a goose, huh?! It's all happening in the class Application\Module
Well, to change a little onBootstrap() application:
What else to add? I think that you should Blitz templates save with the extension .phtml and pile up changed config:
Well, I had cuuute-just change the controller code where the view is passed the result set from the database: Blitz does not support all those fancy iterators, arrays give him (our grandfathers without iterators programmed, and we will! However, fisher?) Along the way, he discovered that if you call toArray() instance ResultSet, then it will call the same toArray() on each row. Had to update the model Album method toArray(). And, well, still had the templates to translate to Blitz and keep with the suffix "blitz.html".
Don't even know how bad this method of integration of a template engine in ZF2. Maybe the professionals know a more sensible way? Discuss?
Article based on information from habrahabr.ru
I remembered then that a long time ago (when the DOM tree was large, and Unicode single-byte) ashamed to have made a tiny commit the Zend Framework (still only 1 version was). Let me think, see — what lives, what breathes this Zend today. Look — up-how... grown up... And I remember it so high... Began to read, what will happen, and even article about the PSR-7 HTTP Messages and their implementation in PHP. Oh, my goodness! We must continue to look, we have to learn, learn and learn. And then come the young punks, so how can you wipe me off the face of the Earth:
All had to learn all over again. I grunted, taken Getting Started
Oh! In ZF2 there is in fact a template engine in PHP!
Over the years, Badoo templates Blitz became familiar and family. We need them here urgently screwed. Googled — no. What the hell?? (maybe I haven't googled it?) In General, clean
The patient was made by application PDF. More precisely, fork.
Tek-C, view... What is a healthy liver in a patient, don't you think, colleague? Even in the coffin put... that is what we transplant. I am under the liver entail RendererStrategy of the Zend View Quick Start. Take a class AcceptStrategy and copied to yourself. Let's call the copy, as usual, Polwart. Just kidding, we will call Application\View\RenderingStrategy. Why not BlitzRenderingStrategy? Yes, sobsno, it creates and configures our rendering engine, which means that it will be the same, for example, to customize the rendering JSON in the case of the relevant request. So here it is.
The resulting class I amputated properties "xxxRenderer" — and why do we need immediately? Well, to have on hand sendowski ServiceManager, decided to pass it in the constructor:
/**
* @param ServiceManager $sm
*/
public function __construct(ServiceManager $sm = null)
{
$this->sm = $sm;
}
selectRenderer() and injectResponse() had to rewrite. That's what you get!
/**
* @param ViewEvent $e
* @return \Zend\View\Renderer\RendererInterface
*/
public function selectRenderer($e)
{
$model = $e->by getmodel();
if ($model instanceof JsonModel) {
return $this->sm->get(JsonRenderer::class);
return $this->sm->get(BlitzRenderer::class);
}
/**
* @param \Zend\Mvc\MvcEvent $e The MvcEvent instance
* @return void
*/
public function injectResponse($e)
{
$renderer = $e->getRenderer();
$response = $e->getResponse();
if ($renderer instanceof JsonRenderer) {
// JSON Renderer; set content-type header
$headers = $response->getHeaders();
$headers->addHeaderLine('content-type', 'application/json');
}
// Inject the content
$response->setContent($e->getResult());
}
While the images were not.
Here. Already the whole class did! But it was not enough! It is necessary that our "module" (a word what — moodul) were aware of the fact that we are preparing! What a goose, huh?! It's all happening in the class Application\Module
/**
* @param \Zend\Mvc\MvcEvent $e The MvcEvent instance
* @return void
*/
private function registerRenderingStrategy($e) {
$app = $e->getTarget();
$locator = $app->getServiceManager();
$view = $locator->get(\Zend\View\View::class);
$renderingStrategy = $locator->get(RenderingStrategy::class);
// Attach strategy, which is a listener aggregate, at high priority
$view->getEventManager()->attach($renderingStrategy, 100);
}
public function getServiceConfig()
{
return array(
...
'factories' => array(
RenderingStrategy::class => function ($sm) {
/** @var ServiceManager $sm */
return new RenderingStrategy($sm);
},..
);
}
Well, to change a little onBootstrap() application:
$eventManager->attach('render', function($e) {
/** @var \Zend\Mvc\MvcEvent $e */
return $this->registerRenderingStrategy($e);
}, 100);
What else to add? I think that you should Blitz templates save with the extension .phtml and pile up changed config:
'view_manager' => array(
...
'default_template_suffix' => 'blitz.html',
),
Well, I had cuuute-just change the controller code where the view is passed the result set from the database: Blitz does not support all those fancy iterators, arrays give him (our grandfathers without iterators programmed, and we will! However, fisher?) Along the way, he discovered that if you call toArray() instance ResultSet, then it will call the same toArray() on each row. Had to update the model Album method toArray(). And, well, still had the templates to translate to Blitz and keep with the suffix "blitz.html".
Don't even know how bad this method of integration of a template engine in ZF2. Maybe the professionals know a more sensible way? Discuss?
Комментарии
Отправить комментарий