Phalcon备份视图路径 [英] Phalcon backup view path

查看:73
本文介绍了Phalcon备份视图路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以通过二级路径到达phalcon中的views目录?

Is there any way to pass through a secondary path to the views dir in phalcon?

在zend框架中,我认为语法是

in zend framework I think the syntax is

$this->view->addScriptPath('/backup/path');
$this->view->addScriptPath('/preferred/path');

因此,如果在首选路径中有一个文件,它将使用它,如果没有,它将通过链回退.

so if there is a file in the preferred path it will use it, if not it will fallback through the chain.

例如,当大多数页面都相同时,我将其用于移动版本,但是有些页面必须有很大不同,并且我不想只复制2个或3个变体的所有视图

I use this, for example, for mobile versions when most of the pages are the same, but some have to be significantly different and I don't want to have to duplicate all the views just for 2 or 3 variants

在phalcon中,我尝试向视图发送数组,但这只会导致两者均无效

In phalcon I have tried sending an array to the view, but that just results in neither working

$di->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir( array('/preferred/path/', '/backup/path/') );
    return $view;
});

推荐答案

通过扩展Phalcon\Mvc\View\Engine\Volt

render($template_path, $params, $must_clean = null)方法中,我设置了替代路径,检查文件是否可用,如果有,我切换用替代路径提供的$ template_path.那么这只是调用的一种情况:

In the render($template_path, $params, $must_clean = null) method I set the alternative path, check if file is available and if so I switch the $template_path given with the alternative path. Then it's just a case of calling:

return parent::render($template_path, $params, $must_clean);

其中$ template_path包含新的(替代)路径.

where $template_path contains the new (alternative) path.

如果您的替代路径可能会因项目而异,而您需要在引导程序中进行设置,那么当您从di获得视图"时,您不必这么做,而在获得伏特时会这样做.

If your alternative path might change on a per project basis and you need to set it in bootstrap, then rather than doing it when getting a "view" from di you would do it when getting volt.

请记住,所有视图都是使用该方法呈现的,因此您还必须考虑布局和部分视图-取决于实现方式.

Just remember that all views are rendered with that method so you will have to account for layout and partial views as well - depending on your implementation.

示例:((未经测试,它基于我在自己的代码中进行的类似设置)

Example: (this has not been tested, it's based on a similar set up I have in my own code)

<?php

class Volt extends Phalcon\Mvc\View\Engine\Volt
{
    private $skin_path;

    public function render($template_path, $params, $must_clean = null)
    {

        $skin_template = str_replace(
            $this->di->getView()->getViewsDir(),
            $this->getSkinPath(),
            $template_path
        );

        if (is_readable($skin_template)) {
            $template_path = $skin_template;
        }

        return parent::render($template_path, $params, $must_clean);
    }

    public function setSkinPath($data)
    {
        $this->skin_path = $data;
    }

    public function getSkinPath()
    {
        return $this->skin_path;
    }
}

在您的引导程序中:

$di->setShared('volt', function($view, $di) {

    $volt = new Volt($view, $di);

    $volt->setSkinPath('my/alternative/dir/');

    return $volt;
});

非常感谢nickolasgregory @ github向我指出了正确的方向.

Many thanks to nickolasgregory@github who pointed me in the right direction.

这篇关于Phalcon备份视图路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆