簡體   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