good afternoon and happy coding with coffee, I want to parse content article to view look beatifull.
While entry content article I’m used jQuery WySiWyG Simditor and successfull saved them.
below sample code to parser HTML code
- in Controller
public function view($id = null)
{
$article = $this->Articles->get($id, [
'contain' => ['Users', 'Comments', 'Tags']
]);
$parsearticle = $this->Articles->find()->extract('Isi');
return $this->response
->withType('application/json')
->withStringBody(json_encode([
'parseHTML' => $parsearticle,
]));
$this->set('article', $article);
}
- in table method
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
foreach($data as $key => $article) {
if(is_string($article)) {
$data[$key] = trim(stripslashes($article));
}
}
}
- in ctp
i’ve read tutorial based on
https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834/2
and also read in jQuery API documentation
https://api.jquery.com/jquery.parsehtml/
<div id="parseHTML">
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
var tampil = $("#parseHTML");
var parse = '<?= Router::url(["controller" => "articles","action" => "view"]); ?>';
var konv = $.parseHTML(parse);
$.ajax({
type:'post',
url:parse,
data:'&type=parseHTML',
dataType:'json',
success:function(){
tampil.append(konv);
});
});
</script>
The output that I’m expect to look beauty article beelow this which content HTML code to formated layout pargraf, indentation and more
the result in view ctp all content JSON formated, below this
I hope someone help me to fix this, thanx verry much and happy coding