Skip to content

Instantly share code, notes, and snippets.

@bfodeke
Created January 18, 2017 16:45
Show Gist options
  • Save bfodeke/253f40451bcd8836f101ec74c3f6e681 to your computer and use it in GitHub Desktop.
Save bfodeke/253f40451bcd8836f101ec74c3f6e681 to your computer and use it in GitHub Desktop.
Demonstrates how to create a theme function in javascript.
(function ($) {
// Declare the theme function.
Drupal.theme.prototype.projectTeaser = function(image_url, title, url) {
return '<div class="teaser"><img class="teaser--image" src="' + image_url + '" /><h2 class="teaser--title">' + title + '</h2><a class="teaser--link" href="' + url +'">View Project</a>';
}
Drupal.behaviors.gtpePurchaseOrder = {
attach: function (context, settings) {
// Call the theme function like so. It should return themed markup.
var teaser = Drupal.theme('projectTeaser', 'http://placehold.it/300x300', 'I did this fancy stuff', 'http://npr.org');
// Append it to a div.
$('.project-teaser').append(teaser);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment