Lithium particles

I usually use the Zend Framework, and this is what I missed in Lithium. Partials. There is a rendering method in the view in which you can use the โ€œelementsโ€ that are closest to me.

<?php $this->_render('element', 'form); ?> 

This works, but it requires the form.html.php file to be in the / views / elements folder. Is it possible to look for it in a different way? Like / views / users /, it gets the file /views/users/form.html.php.

I tried the following, as I found out that the render method accepts a parameter argument in which you can specify a path. So I made an assistant to solve this problem for me.

 namespace app\extensions\helper; use lithium\template\TemplateException; class Partial extends \lithium\template\Helper { public function render($name, $folder = 'elements', $data = array()) { $path = LITHIUM_APP_PATH . '/views/' . $folder; $options['paths']['element'] = '{:library}/views/' . $folder . '/{:template}.{:type}.php'; return $this->_context->view()->render( array('element' => $name), $data, $options ); } } 

However, it still searches only in the / view / elements folder, and not in the path you specify.

Is there something I'm doing wrong?

+6
source share
3 answers

Why use plugins if this material can be hoped to be made with lithium :-)

I do not know Zend, but here is an example of how to configure the default paths for elements in different ways, so that they can be loaded from the linked view folder instead of the common path.

And add one more thing: we want to distinguish elements / partial from the normal view by adding un underscore to the file name (mimic Rails partials)

Migrate Media first during the boot process (config / bootstrap / media.php)

 Media::type('default', null, array( 'view' => 'lithium\template\View', 'paths' => array( 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php', 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php', 'element' => array( '{:library}/views/{:controller}/_{:template}.{:type}.php', '{:library}/views/elements/{:template}.{:type}.php' ) ) )); 

Then use

Suppose the controller is Documents . Call to view:

 <?= $this->_render('element', 'foo', $data, array('controller' => 'documents')); ?> 

This will look for the file inside views/documents/_foo.html.php , and if it does not exist, go back to /views/elements/foo.html.php

Such a simple rearrangement of the default values โ€‹โ€‹in the framework can be done in Lithium for a set of materials (default controller paths for creating namespaces, presentation paths, libraries, etc.)

Another example is to archive your template templates so that you can have things like pages/users_{username}.php instead of the default lithium value: https://gist.github.com/1854561

+11
source

Fixed. It works like a charm. Zend as Partials in Lithium.

 <?php namespace app\extensions\helper; use lithium\template\View; class Partial extends \lithium\template\Helper { public function render($name, $folder = 'elements', array $data = array()) { $view = new View(array( 'paths' => array( 'template' => '{:library}/views/' . $folder . '/' . $name . '.{:type}.php' ) )); return $view->render('all', $data); } } 

It can be used in templates, such as:

 <?php echo $this->partial->render('filename', 'foldername', compact('foo', 'bar')); ?> 
+2
source

There is a plugin for partial ones. https://github.com/dmondark/li3_partials

+1
source

Source: https://habr.com/ru/post/915656/


All Articles