退一步,将您的问题解释为“我如何在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>
这很有趣。感谢您使用更多信息更新您的问题。虽然我不太理解'getTemplatePath()'的用法。你是否想弄清楚如何获得通过'addModule()'选项传递的元素模板的路径?你可以在你的Block助手中使用'$ this - > _ render('element',$ template)'吗?这将使用Media类来查找模板路径并将其呈现为html - http:// li3。me/docs/lithium/template/view/Renderer :: _ render() – rmarscher
谢谢你的问题。我不能在块帮助器中调用$ this - > _ render。它不起作用。我也试过调用$ this - > _ contect - > _ render函数,它也不起作用。 我必须这样做来渲染元素: $ view = $ this - > _ context-> view(); 然后我打电话给 $ view-> render('element',$ params); 我想获得模板路径的原因是我希望每个模板包都可以有不同的方式来渲染块(Joomla中的模块位置)。所以我定义了一个ModuleRenderer类,然后将该文件放在模板文件夹中。 我与Joomla合作多年,我喜欢它呈现模板的方式。 –
好的谢谢。你能发布一个链接到描述这个功能的Joomla文档吗?我可以看到是否有更清洁的方式来做锂电。 – rmarscher