简体   繁体   English

Phalcon备份视图路径

[英]Phalcon backup view path

Is there any way to pass through a secondary path to the views dir in phalcon? 有什么办法可以通过二级路径到达phalcon中的views目录?

in zend framework I think the syntax is 在zend框架中我认为语法是

$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. 因此,如果首选路径中有一个文件,它将使用它,如果不是,它将通过链回退。

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 例如,当大多数页面相同时,我会将它用于移动版本,但有些页面必须明显不同,我不想仅仅针对2或3个变体复制所有视图

In phalcon I have tried sending an array to the view, but that just results in neither working 在phalcon中,我尝试向视图发送数组,但这只会导致两者均不起作用

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

I've got this working by extending the Phalcon\\Mvc\\View\\Engine\\Volt 我通过扩展Phalcon\\Mvc\\View\\Engine\\Volt此工作

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. render($template_path, $params, $must_clean = null)方法中,我设置了替代路径,检查文件是否可用,如果是,我切换给出了替换路径的$ template_path。 Then it's just a case of calling: 然后它只是一个调用的情况:

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

where $template_path contains the new (alternative) path. 其中$ template_path包含新的(替代)路径。

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. 如果您的替代路径可能会因项目而异,而您需要在引导程序中进行设置,那么当从di获得“视图”时,您可以执行此操作,而不是在获得电压时执行此操作。

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;
    }
}

In your bootstrap: 在您的引导程序中:

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

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

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

    return $volt;
});

Many thanks to nickolasgregory@github who pointed me in the right direction. 非常感谢nickolasgregory @ github向我指出了正确的方向。

Method proposed by @strayobject helps me also, but I've found that using extend or other statements inside volt templates dosn't work. @strayobject提出的方法也帮助了我,但我发现在伏特模板中使用extend或其他语句不起作用。

Here's refined solution that works with extend and include : 这是与extend一起使用的精致解决方案, include

use Phalcon\Mvc\View\Engine\Volt;

class VoltExtension extends Volt
{
    // Override default Volt getCompiler method
    public function getCompiler()
    {
        if (!$this->_compiler) {
            $this->_compiler = new VoltCompilerExtension($this->getView());
            $this->_compiler->setOptions($this->getOptions());
            $this->_compiler->setDI($this->getDI());
        }
        return $this->_compiler;
    }
}

And

use Phalcon\Mvc\View\Engine\Volt;

class VoltCompilerExtension extends Volt\Compiler
{
    public function compileFile($path, $compiledPath, $extendsMode = null)
    {
        $skinPath = $this->getOption('skinPath');
        if ($skinPath) {
            $skinTemplate = str_replace(
                $this->getDI()->getView()->getViewsDir(),
                $skinPath,
                $path
            );

            if (is_readable($skinTemplate)) {
                $path = $skinTemplate;
            }
        }

        return parent::compileFile($path, $compiledPath, $extendsMode);
    }

}

Usage: 用法:

$volt = new VoltExtension($view, $di);
$volt->setOptions(
    array(
        'compiledPath' => $config->application->cacheDir,
        'compiledSeparator' => '_',
        'compileAlways' => false,
        'skinPath' => $config->application->skinPath
     )
 );

Please take a look at this phalcon framework update. 请查看此Phalcon框架更新。 It provides support for multiple view packages per website (you can have multiple websites). 它为每个网站提供多个视图包的支持(您可以拥有多个网站)。 Users of the magento framework will find it easy to use: magento框架的用户会发现它易于使用:

https://github.com/alanbarber111/cloud-phalcon-skeleton https://github.com/alanbarber111/cloud-phalcon-skeleton

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

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