We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6491
    • 70 Posts
    I have the following code in my template, how should I get it to work?

    <div id="weatherBox">
    <img class="weatherTodayImg" src="<?php echo $weather->get_condition_image(); ?>" width="" height="" alt=""/>
    <div class="weatherTodayText">
    <h1><?php echo $weather->get_condition(); ?></h1>
    <p><?php echo $weather->get_temperature(); ?>°
    <?php echo $weather->get_units_temp(); ?> (feels like <?php echo $weather->get_wind_chill(); ?>°)</p>

    <p><strong>Wind:</strong> <?php echo $weather->get_wind_speed(); ?><?php echo $weather->get_units_speed(); ?>, <?php echo $weather->get_wind_direction(); ?></p>
    <p><strong>Sunrise:</strong> <?php echo $weather->get_sunrise(); ?><strong>

    Sunset:</strong> <?php echo $weather->get_sunset(); ?></p>
    </div>
    <?php foreach ($weather->get_forecasts() as $forecast): ?>

    <div class="weatherForecast">
    <!--<p><img src="<?php echo $forecast->get_image(); ?>" width="40" height="40" alt=""/></p>-->
    <p><strong>
    <?php echo $forecast->get_date('l') ?></strong>

    Low: <?php echo $forecast->get_low(); ?>

    High: <?php echo $forecast->get_high(); ?>

    <?php echo $forecast->get_label() ?>
    </p>
    </div>
      • 4172
      • 5,888 Posts
      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:

      [[!weather]]


      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.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 6491
        • 70 Posts
        Thanks Bruno,

        I really don't understand PHP, but maybe you can step me through. I'll buy you a beer or two.
        I created the chunks and the snippet by cutting and pasting your code.
        When I test the page it produces this error ...
        "Fatal error: Class 'Whatever' not found in /home/content/13/11916813/html/core/cache/includes/elements/modsnippet/4.include.cache.php on line 10"
        ... I realize I should replace the Whatever with something but I don't know what.

        Thanks again.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          The $weather->get...() functions mean that "$weather" is an object; so you have to include a file defining that object and all of its functions somewhere. Where does your code come from originally? Is it something that we can take a look at?
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 6491
            • 70 Posts
            Quote from: sottwell at Dec 12, 2013, 10:35 PM
            The $weather->get...() functions mean that "$weather" is an object; so you have to include a file defining that object and all of its functions somewhere. Where does your code come from originally? Is it something that we can take a look at?

            Yes of course. I started with this ... http://simplepie.org/wiki/addons/yahoo_weather

            My non-Modx page is working here ... http://emiwip.com/anchor/index.php -and here- http://emiwip.com/anchor/weather.php

            Thanks for all your help.
              • 4172
              • 5,888 Posts
              lets say you have the simplepie - files under
              core/components/simplepie/model/simplepie/

              then try my modified version above. [ed. note: Bruno17 last edited this post 12 years, 9 months ago.]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 6491
                • 70 Posts
                Bruno, like this? ...
                  • 4172
                  • 5,888 Posts

                  The files should be under core/components/simplepie

                  not core/components/model/

                  Then change that part to the correct path, where the files are:

                  $model_path = $modx->getOption('core_path') . 'components/simplepie/model/simplepie/' ;
                    
                  require_once($model_path . 'simplepie.inc');
                  require_once($model_path . 'simplepie_yahoo_weather.inc');


                  reading here:

                  http://simplepie.org/wiki/addons/yahoo_weather

                  the file
                  simplepie_yahoo_weather.inc

                  should be on the same folder as your
                  simplepie.inc

                  I couldn't find this file at your pic.





                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 6491
                    • 70 Posts
                    Hi Bruno,
                    the file
                    simplepie_yahoo_weather.inc

                    should be on the same folder as your
                    simplepie.inc
                    The simplepie.inc file has been replaced by autoloader.php, in the newer version of SimplePie ... (http://simplepie.org/wiki/setup/sample_page).

                    I replaced your code as follows ...
                    $model_path = $modx->getOption('core_path') . 'components/simplepie/model/simplepie/' ;
                       
                    require_once($model_path . 'autoloader.php'); // Note: autoloader.php not simplepie.inc
                    require_once($model_path . 'simplepie_yahoo_weather.inc');

                    ... and placed the weather snippet on line one of my template. I get 2 errors, see them on this page ... http://www.beavercreekmarina.com/index.php?id=2

                    Thanks man.