Last active
October 14, 2015 19:45
-
-
Save uncatcrea/02c59fb187eecf8e58e7 to your computer and use it in GitHub Desktop.
This example shows how to use a custom template ("my-component-template.html") for a given component ("my-component-slug") and how to retrieve component's data from template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//In theme's functions.js | |
/** | |
Use a custom 'my-component-template.html' template only for the | |
'my-component-slug' component : | |
*/ | |
App.filter( 'template', function( template, current_screen ) { | |
if ( current_screen.component_id == 'my-component-slug' ) { | |
template = 'my-component-template'; //Don't need .html here. | |
} | |
return template; | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% // In theme's "my-component-template.html" %> | |
<h1>My customized component's template!</h1> | |
<% | |
//As we use this template for the "my-component-slug" component, | |
//we can use the "posts" global var, that contains our component's posts : | |
console.log('Component posts', posts); //Array of JSON posts | |
//And/or retrieve any other (post list) component's posts : | |
var other_component = TemplateTags.getComponent('my-other-component-slug'); | |
var other_component_posts = other_component.view_data.posts; //Backbone Collection of posts | |
var other_component_posts_array = other_component_posts.toJSON(); //Array of JSON posts | |
console.log("Other component's posts", other_component_posts_array); | |
//Then implement _.each() loops on posts arrays to display post lists as you | |
//would do in the archive.html template | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment