I’m trying to load blog content dynamically, from what I understand AJAX is the way to do this. Unfortunately my understanding of Javascript is not very good, so any help to implement what I’m trying to achieve would be greatly appreciated.
Here is what I have so far.
This is the code I have, the first div to show the list of articles and the second div is where I want the content to load when an article link is clicked from the list.
<div class="curation-contents">
<?php foreach($site->page('blog')
->children()
->visible()
->flip() as $article): ?>
<?php snippet('article', array('article' => $article)) ?>
<?php endforeach ?>
</div>
<div class="article-container">
</div>
Here is the article snippet:
<article class="article-item">
<a class="load" data-target="#article-container" href="<?= $article->url() ?>">
<h3 ><?php echo html($article->title()) ?></h3>
</a>
<p><?php echo kirbytext($article->intro()) ?></p>
<time><?php echo $article->date('d/m/Y') ?></time>
</article>
Is JSON the right tried to go about creating a JSON template but I don’t know whether it’s right or whether this is this is the right way to approach my problem,
<?php
$data = [
'title' => $page->title()->value(),
'text' => $page->text()->kirbytext(),
'date' => $page->date('d/m/Y'),
'image' => $page->coverimage()->toFile()->url()
];
echo json_encode($data);
?>
This is my AJAX call so far:
$(function(){
var element = $('.load');
var url = element.data('article') + '.json';
$('.load').on('click', function(e) {
e.preventDefault();
$.ajax({
type: "GET",
dataType: "json",
url: '',
data: data,
success: function(data) {
},
});
});
Any pointers in the right direction would be welcome