Skip to content

Instantly share code, notes, and snippets.

@sumitk
Forked from juampynr/html.tpl.php
Created October 18, 2013 21:45
Show Gist options
  • Select an option

  • Save sumitk/7048704 to your computer and use it in GitHub Desktop.

Select an option

Save sumitk/7048704 to your computer and use it in GitHub Desktop.
name = My module
description = TODO: Description of module
core = 7.x
/**
* Renders the weather status for a city.
*/
var app = angular.module('myapp', [])
.controller('MyModuleWeather', function($scope, $http) {
$http.jsonp('http://api.openweathermap.org/data/2.5/weather?q=London,uk&callback=JSON_CALLBACK').
success(function(data, status, headers, config) {
$scope.main = data.main;
$scope.wind = data.wind;
$scope.description = data.weather[0].description;
}).
error(function(data, status, headers, config) {
// Fail silently.
});
});
<?php
/**
* @file mymodule.module
* Spike solution to implement an AngularJS-driven block.
*/
/**
* Implements hook_block_info().
*/
function mymodule_block_info() {
$blocks['weather'] = array(
'info' => t('Weather'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function mymodule_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'weather':
$path = drupal_get_path('module', 'mymodule');
$block['subject'] = t('Weater status');
$block['content'] = array(
'#markup' => theme('weather_status'),
'#attached' => array(
'js' => array(
'https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js',
$path . '/mymodule.js',
),
),
);
break;
}
return $block;
}
/**
* Implements hook_theme().
*/
function mymodule_theme() {
return array(
'weather_status' => array(
'template' => 'weather-status',
),
);
}
<?php
/**
* @file
* AngularJS template to render a weather block.
*/
?>
<div ng-controller="MyModuleWeather">
<h3>{{data.name}}</h3>
<p>{{description}}</p>
<p>Temperature: {{main.temp}}</p>
<p>Wind speed: {{wind.speed}}</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment