you can't run php-code directly in a template for good reason.
You need to create a snippet.
The MODX-way would look something like that:
your snippet 'weather'
<?php
$p = $modx->getOption('p',$scriptProperties,'USCA0987');
$tpl = $modx->getOption('tpl',$scriptProperties,'tplWeather');
$forecastTpl = $modx->getOption('forecastTpl',$scriptProperties,'tplForecast');
$model_path = $modx->getOption('core_path') . 'components/simplepie/model/simplepie/' ;
require_once($model_path . 'simplepie.inc');
require_once($model_path . 'simplepie_yahoo_weather.inc');
// Initialize a new SimplePie object.
$feed = new SimplePie();
// Parse a Yahoo! Weather feed for San Francisco, CA
$feed->set_feed_url('http://weather.yahooapis.com/forecastrss?p='.$p);
// We're going to override the built-in SimplePie_Item class with the SimplePie_Item_YWeather class.
$feed->set_item_class('SimplePie_Item_YWeather');
// Initialize the feed
$feed->init();
// Since Y! Weather feeds only have one item, we'll base everything around that.
$weather = $feed->get_item(0);
$fc_out = '';
foreach ($weather->get_forecasts() as $forecast){
$fc = array();
$fc['image'] = $forecast->get_image();
$fc['date'] = $forecast->get_date('l');
$fc['low'] = $forecast->get_low();
$fc['high'] = $forecast->get_high();
$fc['label'] = $forecast->get_label();
$fc_out .= $modx->getChunk($forecastTpl,$fc);
}
$w = array();
$w['forecasts'] = $fc_out;
$w['condition_image'] = $weather->get_condition_image();
$w['condition'] = $weather->get_condition();
$w['temperature'] = $weather->get_temperature();
$w['units_temp'] = $weather->get_units_temp();
$w['wind_chill'] = $weather->get_wind_chill();
$w['wind_speed'] = $weather->get_wind_speed();
$w['units_speed'] = $weather->get_units_speed();
$w['wind_direction'] = $weather->get_wind_direction();
$w['sunrise'] = $weather->get_sunrise();
$w['sunset'] = $weather->get_sunset();
$output = $modx->getChunk($tpl,$w);
return $output;
?>
you would call the snippet in your template like that:
and have two chunks.
chunk 'tplWeather':
<div id="weatherBox">
<img class="weatherTodayImg" src="[[+condition_image]]" width="" height="" alt=""/>
<div class="weatherTodayText">
<h1>[[+condition]]</h1>
<p>[[+temperature]]°
[[+units_temp]] (feels like [[+wind_chill]]°)</p>
<p><strong>Wind:</strong> [[+wind_speed]][[+units_speed]], [[+wind_direction]]</p>
<p><strong>Sunrise:</strong> [[+sunrise]]<strong>
Sunset:</strong> [[+sunset]]</p>
</div>
[[+forecasts]]
</div>
chunk 'tplForecast':
<div class="weatherForecast">
<!--<p><img src="[[+image]]" width="40" height="40" alt=""/></p>-->
<p><strong>[[+date]]</strong>
Low: [[+low]]<br />
High: [[+high]]<br />
[[+label]]
</p>
</div>
[ed. note: Bruno17 last edited this post 12 years, 9 months ago.]