[英]How to avoid processing HTML outside of the template for Joomla components?
I create my own component in Joomla 3.x I have a functionnal Ajax call like this : 我在Joomla 3.x中创建了自己的组件,我有一个像这样的Analx函数调用:
file.js 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 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
}
I'd like to use the result ($myData = result PDO::FETCH_CLASS) in a view. 我想在视图中使用结果($ myData =结果PDO :: FETCH_CLASS)。 ($mydataSQL->name, $mydataSQL->address...) to avoid processing the html in the controller function. ($ mydataSQL-> name,$ mydataSQL-> address ...),以避免在控制器函数中处理html。
I tryed without success a call like this : ...&format=raw&view=newview. 我尝试了类似的呼叫:...&format = raw&view = newview。
What you want here is to use a layout. 您要在这里使用布局。
https://docs.joomla.org/J3.x:Sharing_layouts_across_views_or_extensions_with_JLayout https://docs.joomla.org/J3.x:Sharing_layouts_across_views_or_extensions_with_JLayout
in your controller 在您的控制器中
private function formatHtml(MyClassFoo $foo)
{
$layout = new JLayoutFile('path.to.layout');
$data = array('foo' => $foo);
return $layout->render($data);
}
and in layouts/path/to/layout.php (or templates/#current template#/html/layouts/path/to/layout.php) 并在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.