Lithium:如何从Helper获取布局/模板路径值

时间:2014-02-28 21:32:45

标签: lithium

我有一个辅助类,它扩展了\ lithium \ template \ Helper。我怎么知道用于渲染的布局文件/路径?

谢谢。

更新: 我需要这个的原因是因为我希望该站点支持多个模板包。 模板布局将支持块模块渲染(喜欢Joomla模板),因此在布局文件中我可以这样做:

<?php if($this->Block->countModule('slider')){ ?>
<div id="slider">
    <?php echo $this->Block->renderBlock('slider'); ?>
</div>
<?php } ?>

要将模块添加到块,我执行此操作:

$this->Block->addModule('slider', array('element'=>'slider'));

........................

我必须覆盖渲染器对象 在bootstrap / media.php中

Media::type('html', 'text/html', array(
   'view' => 'app\extensions\template\View'
));

我创建了新文件/app/extensions/template/View.php

class View extends \lithium\template\View {
    public function __construct(array $config = array()) {
        $defaults = array(
            'renderer' => 'app\extensions\template\view\adapter\File'
        );
        parent::__construct($config + $defaults);
    }
}

最后是/app/extensions/template/adapter/File.php

class File extends \lithium\template\view\adapter\File {
    public function getTemplatePath(){
        $path = $this->_paths['layout'][0];
        $path = preg_replace('/\/\{:layout\}.*$/', '', $path);
        return $path;
    }
}

现在我可以走这条路。

2 个答案:

答案 0 :(得分:1)

退后一步,将问题解释为&#34;如何复制Joomla!模板模块在Lithium PHP中的位置?&#34;,我想出了这个解决方案。

请参阅https://gist.github.com/rmarscher/10020347

app\extensions\helper\Module创建一个包含以下内容的视图助手:

<?php

namespace app\extensions\helper;

use lithium\core\Libraries;
use lithium\template\view\TemplateException;
use lithium\template\View;

/**
 * An implementation of Joomla! template module positions for Lithium.
 *
 * Here is how you can render to a module position from one of your inner templates:
 * {{{
 * $this->modules->bottom("element", 'bottomTest');
 * $this->modules->top("simple", "<p>Maybe we can just put some html in there...</p>");
 * }}}
 *
 * To do the same from inside another helper, use `$this->_context->modules()`.
 *
 * Then in your layout file, output the module in the desired location:
 * {{{
 * <body>
 * <?php echo $this->modules->top(); ?>
 * <?php echo $this->content(); ?>
 * <?php echo $this->modules->bottom(); ?>
 * </body>
 * }}}
 *
 * @see http://docs.joomla.org/Creating_a_basic_Joomla!_template#Body_Section
 */
class Modules extends \lithium\template\Helper {

    protected $_rendered = array();
    protected $_simpleView = null;

    public function __call($name, $params) {
        return $this->position($name, $params);
    }

    public function position($name, $params) {
        if (empty($this->_rendered[$name])) {
            $this->_rendered[$name] = "";
        }
        switch (count($params)) {
            case 0:
                return $this->_rendered[$name];
            case 1:
                return $this->_rendered[$name] .= $this->render($params[0]);
            case 2:
                return $this->_rendered[$name] .= $this->render($params[0], $params[1]);
            case 3:
                return $this->_rendered[$name] .= $this->render($params[0], $params[1], $params[2]);
            case 4:
                return $this->_rendered[$name] .= $this->render($params[0], $params[1], $params[2], $params[3]);
            case 5:
                return $this->_rendered[$name] .= $this->render($params[0], $params[1], $params[2], $params[3], $params[4]);
            default:
                return $this->_rendered[$name] .= call_user_func_array(array(&$this, $method), $params);
        }
    }

    /**
     * Shortcut method used to render elements and other nested templates for named module blocks.
     *
     * @see lithium\template\View::render()
     * @param string $type The type of template to render, usually either `'element'` or
     *               `'template'`. Indicates the process used to render the content. See
     *               `lithium\template\View::$_processes` for more info.
     *               There's an additional special option here for the Modules helper.
     *               Use `"simple"` to render a string template rather than from a file.
     * @param string $template The template file name. For example, if `'header'` is passed, and
     *               `$type` is set to `'element'`, then the template rendered will be
     *               `views/elements/header.html.php` (assuming the default configuration).
     *               If `$type === 'simple'`, this should be the template content.
     * @param array $data An array of any other local variables that should be injected into the
     *              template. By default, only the values used to render the current template will
     *              be sent. If `$data` is non-empty, both sets of variables will be merged.
     * @param array $options Any options accepted by `template\View::render()`.
     * @return string Returns a the rendered template content as a string.
     */
    public function render($type, $template, array $data = array(), array $options = array()) {
        $view = $this->_context->view();
        if ($type !== "simple") {
            return $view->render($type, $data, compact('template') + $options);
        }
        if (!$this->_simpleView) {
            $this->_simpleView = new View(array('loader' => 'Simple', 'renderer' => 'Simple'));
        }
        $element = $template;
        return $this->_simpleView->render('element', $data, compact('element') + $options);
    }

}

?>

然后,您可以在模板中执行此操作以渲染到模块位置:

<?=$this->_render("element", "elementTest"); ?>
<?php $this->modules->top("element", 'topTest'); ?>
<?php $this->modules->bottom("element", 'bottomTest'); ?>
<h1>Hi there. I'm the main content.</h1>
<?php $this->modules->top("simple", "<p>Maybe we can just put some html in there...</p>");

然后在布局模板中执行此操作:

<!doctype html>
<html>
<head>
    <?php echo $this->html->charset();?>
    <title><?php echo $this->title(); ?></title>
</head>
<body>
    <div class="content">
        <?php echo $this->modules->top(); ?>
        <?php echo $this->content(); ?>
        <?php echo $this->modules->bottom(); ?>
    </div>
</body>
</html>

答案 1 :(得分:0)

您可以通过将参数中的__FILE__传递给辅助函数来获取模板路径。

您的模板中似乎还有一个内部$template__变量。

请参阅http://li3.me/docs/lithium/template/view/adapter/File::render()

您的帮助程序中还可以使用$this->_context作为Renderer对象。我不认为它存储有关正在呈现哪个文件的状态。