<?php $xml = simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan'); $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); ?> <html> <head> <title>Google Weather API</title> </head> <body> <h1><?php echo $information[0]->city['data']; ?></h1> <h2>Today's weather</h2> <div> <img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" /> <div> <?php echo $current[0]->temp_c['data'] ?>° C, <?php echo $current[0]->condition['data']; ?> </div> </div> </body> </html> Help Appreciated! Thanks!
Since you're talking about Smarty, I presume you're loading the template from a regular PHP file somewhere. In this PHP-file, you should do all your "logic", and then assign the variables for output in the template file. In your original PHP-file, you should do something like this (assuming Smarty is initialized to the variable named $smarty): $xml = simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan'); $information = $xml->xpath("/xml_api_reply/weather/forecast_information"); $current = $xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions"); $smarty->assign(array( 'city' => $information[0]->city['data'], 'temp' => $current[0]->temp_c['data'], 'condition' => $current[0]->condition['data'] )); PHP: In your template file, you would then do like this: <html> <head> <title>Google Weather API</title> </head> <body> <h1>{$city}</h1> <h2>Today's weather</h2> <div> <img src="http://www.google.com{$icon}" /> <div> {$temp}° C, {$condition} </div> </div> </body> </html> HTML: Makes sense?
Hi SoulStone,Thanks for your reply.But,this is not executing the PHP function.I had make 1 .tpl file name as weather.tpl & include it in another .tpl file.Do you have any solution for this. Thanks!
Ah okay. I guess you could do like this: {php} $xml=simplexml_load_file('http://www.google.com/ig/api?weather=Jaipur-Rajasthan'); $information=$xml->xpath("/xml_api_reply/weather/forecast_information"); $current=$xml->xpath("/xml_api_reply/weather/current_conditions"); $forecast_list=$xml->xpath("/xml_api_reply/weather/forecast_conditions"); $this->assign(array( 'city'=>$information[0]->city['data'], 'temp'=>$current[0]->temp_c['data'], 'condition'=>$current[0]->condition['data'] )); {/php} <html> <head> <title>Google Weather API</title> </head> <body> <h1>{$city}</h1> <h2>Today's weather</h2> <div> <img src="http://www.google.com{$icon}" /> <div> {$temp}° C, {$condition} </div> </div> </body> </html> HTML:
Using those {php}{/php}-tags, you can "embed" PHP directly into the template file. This is not very good practice though, but might be the only solution here. You still have to remember to "assign" the variables though.