繁体   English   中英

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

[英]How to avoid processing HTML outside of the template for Joomla components?

我在Joomla 3.x中创建了自己的组件,我有一个像这样的Analx函数调用:

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 =结果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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM