简体   繁体   English

锂的部分

[英]Partials in lithium

Normally I use the Zend Framework and this is something I miss in Lithium. 通常我使用Zend Framework,这是我在Lithium中遗漏的东西。 Partials. 谐音。 There is a render method in the view where you can use 'elements' which is the closest I got. 在视图中有一个渲染方法,你可以使用最接近的'elements'。

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

This does work, however it requires that the form.html.php file is in the /views/elements folder. 这确实有效,但它要求form.html.php文件位于/ views / elements文件夹中。 Is it possible to let it search in another path? 是否可以让它在另一条路径中搜索? Like /views/users/ so it gets the file /views/users/form.html.php. 喜欢/ views / users /所以它获取文件/views/users/form.html.php。

I have tried the following, since I found out that the render method does accept an options argument wherein you can specify a path. 我尝试了以下内容,因为我发现render方法确实接受了一个options参数,您可以在其中指定路径。 So I made an Helper to fix 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 only searches in the /view/elements folder, not in the path I specified. 但是它仍然只在/ view / elements文件夹中搜索,而不是在我指定的路径中搜索。

Is there something I am doing wrong? 有什么我做错了吗?

Why using plugins when this stuff can hopefully be done by Lithium :-) 为什么使用插件,希望这些东西可以由Lithium完成:-)

I don't know Zend, but here is an exemple to configure elements default paths differently, to load them from the related view folder, instead of a shared path. 我不知道Zend,但这里有一个例子来配置元素默认路径,从相关的视图文件夹而不是共享路径加载它们。

And let's add one more thing: we want to differentiate elements/partials from a normal view, by appending un underscore to the name of the file (mimic Rails partials) 让我们再添加一件事:我们希望通过在文件名后附加非下划线来区分元素/部分与普通视图(模仿Rails部分)

First, reconfigure Media during the bootstrap process (config/bootstrap/media.php) 首先,在引导过程中重新配置Media(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 it 然后,使用它

Suppose a controller Documents . 假设一个控制器Documents Call on a view: 呼吁观点:

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

This will look for a file inside views/documents/_foo.html.php and if doesn't exists, fallback to /views/elements/foo.html.php 这将在views/documents/_foo.html.php查找文件,如果不存在,则回/views/elements/foo.html.php

This kind of simple re-configuration of framework defaults, can be done in Lithium for a bunch of stuffs (default controllers paths to create namespaces, views paths, libraries, etc ...) 这种简单的框架默认重新配置,可以在Lithium中完成一堆东西(创建命名空间的默认控制器路径,视图路径,库等......)

One more example to re-maps your template paths so you can have stuff like pages/users_{username}.php instead of the Lithium default: https://gist.github.com/1854561 还有一个示例是重新映射模板路径,以便您可以使用pages/users_{username}.php而不是Lithium默认值: https//gist.github.com/1854561

Fixed it. 修复。 Works like a charm. 奇迹般有效。 Zend like Partials in Lithium. 像锂电池中的Partials一样。

<?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);

    }

}

Can be used in templates like: 可以在以下模板中使用:

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

There is a plugin for partials. 有一个部分插件。 https://github.com/dmondark/li3_partials https://github.com/dmondark/li3_partials

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM