如何避免在 Joomla 组件的模板之外处理 HTML?
How to avoid processing HTML outside of the template for Joomla components?
我在 Joomla 中创建自己的组件 3.x
我有一个像这样的功能性 Ajax 调用:
file.js
jQuery.ajax({
type: 'post',
data: 'topo_id=' + idTopo,
url: 'index.php?option=com_mycomp&task=getMyData&format=json',
datatype: "json",
success: function(res) {
console.log(res);
jQuery('#resultDiv').html(res.data);
},
error: function (e) {
console.log(e);
}})
controller.php
function getMyData(){
$mydataSQL = $MyClass->getMyData($param); //
$mydataHtml = $this->formatHtml($mydataSQL); // to replace div content with ajax
echo new JResponseJson($mydataHtml);
}
function formatHtml(MyClassFoo $foo) {
$html ='<div id="foo">' . $foo->bar . '</div>';
$html .= '<h1>' . $foo->foo .'</h1>';
...... and more html code here
return $html
}
我想在视图中使用结果 ($myData = result PDO::FETCH_CLASS)。 ($mydataSQL->name, $mydataSQL->address...) 以避免在控制器函数中处理 html。
我试过这样的调用但没有成功:...&format=raw&view=newview。
这里你想要的是使用布局。
https://docs.joomla.org/J3.x:Sharing_layouts_across_views_or_extensions_with_JLayout
在你的控制器中
private function formatHtml(MyClassFoo $foo)
{
$layout = new JLayoutFile('path.to.layout');
$data = array('foo' => $foo);
return $layout->render($data);
}
并在 layouts/path/to/layout.php(或模板/#current template#/html/layouts/path/to/layout.php)
<?php
defined('JPATH_BASE') or die;
$foo = $displayData['foo'];
?>
<div id="foo"><?= $foo->bar ?></div>
<h1><?= $foo->foo ?></h1>
<p>and more...</p>
</div>
我在 Joomla 中创建自己的组件 3.x 我有一个像这样的功能性 Ajax 调用:
file.js
jQuery.ajax({
type: 'post',
data: 'topo_id=' + idTopo,
url: 'index.php?option=com_mycomp&task=getMyData&format=json',
datatype: "json",
success: function(res) {
console.log(res);
jQuery('#resultDiv').html(res.data);
},
error: function (e) {
console.log(e);
}})
controller.php
function getMyData(){
$mydataSQL = $MyClass->getMyData($param); //
$mydataHtml = $this->formatHtml($mydataSQL); // to replace div content with ajax
echo new JResponseJson($mydataHtml);
}
function formatHtml(MyClassFoo $foo) {
$html ='<div id="foo">' . $foo->bar . '</div>';
$html .= '<h1>' . $foo->foo .'</h1>';
...... and more html code here
return $html
}
我想在视图中使用结果 ($myData = result PDO::FETCH_CLASS)。 ($mydataSQL->name, $mydataSQL->address...) 以避免在控制器函数中处理 html。
我试过这样的调用但没有成功:...&format=raw&view=newview。
这里你想要的是使用布局。
https://docs.joomla.org/J3.x:Sharing_layouts_across_views_or_extensions_with_JLayout
在你的控制器中
private function formatHtml(MyClassFoo $foo)
{
$layout = new JLayoutFile('path.to.layout');
$data = array('foo' => $foo);
return $layout->render($data);
}
并在 layouts/path/to/layout.php(或模板/#current template#/html/layouts/path/to/layout.php)
<?php
defined('JPATH_BASE') or die;
$foo = $displayData['foo'];
?>
<div id="foo"><?= $foo->bar ?></div>
<h1><?= $foo->foo ?></h1>
<p>and more...</p>
</div>