Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免在Joomla组件的模板之外处理HTML?_Php_Jquery_Joomla - Fatal编程技术网

Php 如何避免在Joomla组件的模板之外处理HTML?

Php 如何避免在Joomla组件的模板之外处理HTML?,php,jquery,joomla,Php,Jquery,Joomla,我在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);

我在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
}
函数getMyData(){ $mydataSQL=$MyClass->getMyData($param);// $mydataHtml=$this->formatHtml($mydataSQL);//用ajax替换div内容 echo新的JResponseJson($mydataHtml); } 函数formatHtml(MyClassFoo$foo){ $html='.$foo->bar'; $html.='.$foo->foo'; ……这里还有更多的html代码 返回$html } 我想在视图中使用结果($myData=result PDO::FETCH_CLASS)。($mydataSQL->name,$mydataSQL->address…)以避免在控制器函数中处理html


我尝试过这样一个调用,但没有成功:…&format=raw&view=newview。

这里需要的是使用布局

在控制器中

private function formatHtml(MyClassFoo $foo) 
{
   $layout      = new JLayoutFile('path.to.layout');
   $data = array('foo' => $foo);

   return $layout->render($data);
}
在layouts/path/to/layout.php(或templates/current template/html/layouts/path/to/layout.php)中


还有更多


没有像
$mydata
这样的变量在当前函数中获取任何值,那么为什么在
JResponseJson
中返回相同的变量呢?是错了还是怎么了?请你详细写下你想对返回值做些什么好吗?@SudhirSapkal抱歉输入错误。我更新了问题。我不想在joomla视图中使用json响应,也不想在我的控制器(函数formatHtml)中使用json响应。您相当注意joomla关于StackOverflow的问题。我希望你也能把你的志愿服务扩展到乔姆拉。(很难找到好的帮助)当然,我有时也会去看看。
<?php
defined('JPATH_BASE') or die;

$foo = $displayData['foo'];
?>
<div id="foo"><?= $foo->bar ?></div>
  <h1><?= $foo->foo ?></h1>
  <p>and more...</p>
</div>