Skip to content

Instantly share code, notes, and snippets.

@caraya
Created March 30, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caraya/5d11e02f4b95e9e6a0c62d4366f03adf to your computer and use it in GitHub Desktop.
Save caraya/5d11e02f4b95e9e6a0c62d4366f03adf to your computer and use it in GitHub Desktop.
Handlebars Wordpress Demo
<div id="myContent"></div>
<template id="posts-list-template">
{{#each posts}}
<div class="post">
<h1>{{title}}</h1>
<div>
{{content.renderes}}
</div>
</div>
{{/each}}
</template>
// https://wpdev.rivendellweb.net/wp-json/wp/v2/posts?per_page=10
let myPosts = fetch('https://wpdev.rivendellweb.net/wp-json/wp/v2/posts?per_page=4')
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
})
.catch((err) => {
console.log('There\'s been an error getting the data', err);
});
let template = document.getElementById('posts-list-template').innerHTML;
let renderPosts = Handlebars.compile(template);
document.getElementById('myContent').innerHTML = renderPosts({
posts: myPosts
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment