2014-02-28 34 views
0

我有一個助手類,它擴展\鋰\模板\助手。我如何知道使用哪個佈局文件/路徑進行渲染?鋰:如何從助手獲得佈局/模板路徑值

謝謝。

更新: 我需要這個的原因是因爲我希望網站支持多個模板包。 模板佈局將支持塊模塊(喜歡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')); 

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

我有覆蓋渲染對象 在自舉/忽略原始

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

現在我可以得到路徑。

+0

這很有趣。感謝您使用更多信息更新您的問題。雖然我不太理解'getTemplatePath()'的用法。你是否想弄清楚如何獲得通過'addModule()'選項傳遞的元素模板的路徑?你可以在你的Block助手中使用'$ this - > _ render('element',$ template)'嗎?這將使用Media類來查找模板路徑並將其呈現爲html - http:// li3。me/docs/lithium/template/view/Renderer :: _ render() – rmarscher

+0

謝謝你的問題。我不能在塊幫助器中調用$ this - > _ render。它不起作用。我也試過調用$ this - > _ contect - > _ render函數,它也不起作用。 我必須這樣做來渲染元素: $ view = $ this - > _ context-> view(); 然後我打電話給 $ view-> render('element',$ params); 我想獲得模板路徑的原因是我希望每個模板包都可以有不同的方式來渲染塊(Joomla中的模塊位置)。所以我定義了一個ModuleRenderer類,然後將該文件放在模板文件夾中。 我與Joomla合作多年,我喜歡它呈現模板的方式。 –

+0

好的謝謝。你能發佈一個鏈接到描述這個功能的Joomla文檔嗎?我可以看到是否有更清潔的方式來做鋰電。 – rmarscher

回答

0

您可以通過將參數__FILE__傳遞給您的幫助函數來獲取模板路徑。

您的模板中似乎還有一個內部$template__變量可用。

請參閱http://li3.me/docs/lithium/template/view/adapter/File::render()

Renderer對象也可以在幫助程序中作爲$this->_context使用。我不認爲它存儲關於哪個文件被渲染的狀態。

+0

我嘗試了$ this - > _ context渲染器對象,但所有存儲有關佈局/模板路徑信息的變量受到保護並且無法訪問。 所以我必須覆蓋渲染器對象 –

1

退一步,將您的問題解釋爲「我如何在Lithium PHP中複製Joomla!模板模塊位置?」,我想出了這個解決方案。

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> 
+0

謝謝。這與我的解決方案非常相似。 –