Skip to content

Instantly share code, notes, and snippets.

@mraichelson
Created March 17, 2014 01:55
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 mraichelson/9592655 to your computer and use it in GitHub Desktop.
Save mraichelson/9592655 to your computer and use it in GitHub Desktop.
simple_weather experiment for providing configurable markup options.
/**
* @file
* JavaScript settings for jQuery simpleWeather plugin.
*
* Download simpleWeather jQuery plugin: http://simpleweatherjs.com.
* Read README.txt for installation instructions.
*/
(function($) {
Drupal.behaviors.simpleWeather = {
attach: function (context, settings) {
/* Block Settings */
var zipCode = Drupal.settings.simple_weather.zipCode;
var woeid = Drupal.settings.simple_weather.woeid;
var scale = Drupal.settings.simple_weather.scale;
/**
* Output markup control.
* Any template markup can use the [weather.foo] token patterns
* shown below in $.simpleWeather.success().
*/
var swMarkup = Drupal.settings.simple_weather.markup;
if (typeof Drupal.settings.simple_weather.custom_markup != 'undefined') {
swMarkup = Drupal.settings.simple_weather.custom_markup;
}
/* The simple weather widget settings */
$.simpleWeather({
zipcode: zipCode,
woeid: woeid,
location: '',
unit: scale,
success: function(weather) {
swMarkup = swMarkup
.replace('[weather.city]', weather.city)
.replace('[weather.region]', weather.region)
.replace('[weather.image]', weather.image)
.replace('[weather.temp]', weather.temp)
.replace('[weather.units.temp]', weather.units.temp)
.replace('[weather.currently]', weather.currently)
.replace('[weather.link]', weather.link);
$("#simple-weather").html(swMarkup);
},
error: function(error) {
$("#simple-weather").html('<p>' + error + '</p>');
}
});
}
};
})(jQuery);
<?php
/**
* Content for the Simple Weather Report block.
*/
function simple_weather_content() {
// Check for required simpleWeather jQuery plugin.
simple_weather_file_required();
// JavaScript and Styles for the Simple Weather Block.
drupal_add_css(drupal_get_path('module', 'simple_weather') . '/css/simple_weather.css');
drupal_add_js(drupal_get_path('module', 'simple_weather') . '/js/simple_weather.js');
// Pass the block settings to the simpleWeather.
drupal_add_js(array('simple_weather' => array('zipCode' => variable_get('simple_weather_zipcode'))), 'setting');
drupal_add_js(array('simple_weather' => array('woeid' => variable_get('simple_weather_woeid'))), 'setting');
drupal_add_js(array('simple_weather' => array('scale' => variable_get('simple_weather_scale'))), 'setting');
// Example of moving markup to JS so it can be overridden.
$default_markup = array();
$default_markup[] = '<h2>[weather.city], [weather.region]</h2>';
$default_markup[] = '<img style="float:left;" width="125px" src="[weather.image]">';
$default_markup[] = '<div class="report">[weather.temp]&deg; [weather.units.temp]<br />';
$default_markup[] = '<span>[weather.currently]</span></div>';
$default_markup[] = '<a href="[weather.link]" title="Yahoo! Weather Forcast">';
$default_markup[] = t('View Forecast &raquo;');
$default_markup[] = '</a>';
drupal_add_js(array('simple_weather' => array('markup' => implode('', $default_markup))), 'setting');
return theme('simple_weather_output');
}
name = My Temporary Module
description = For experimenting with simple_weather.
core = 7.x
dependencies[] = simple_weather
<?php
/**
* @file
*/
/**
* Implements hook_preprocess_HOOK() as hook_preprocess_html().
* This could also be done as part of a theme as part of theme_preprocess_html().
*/
function tempmodule_preprocess_html(&$vars) {
$custom_markup = '<p>It is <strong>[weather.temp]&deg;</strong> in <strong>[weather.city]</strong> right now.</p>';
drupal_add_js(array('simple_weather' => array('custom_markup' => $custom_markup)), 'setting');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment