Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Foreach in php und in tpl ausgeben

 
Post new topic   Reply to topic    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Sun May 29, 2016 11:51 am    Post subject: Foreach in php und in tpl ausgeben Reply with quote

Hallo,

Ich arbeite gerade an einer API die das Wetter ausliesst und diese wird normal so abgerufen (in php):
Code:
foreach($conditions_today as $cond) {

    $echo $cond->getTime('H') . ': ' . $cond->getTemperature(). $cond->getHumidity()*100 . $cond->getSummary() . "<br>";

}


Wie kann ich das jetzt in PHP auslesen und im Template ausgeben?

Ich hatte es probiert

Code:
foreach($conditions_today as $cond) {

    $row =  $cond->getTime('H') . ': ' . $cond->getTemperature(). $cond->getHumidity()*100 . $cond->getSummary() . "<br>";

}

$smarty->assign('row, $row);


Im tpl:
Code:
{$row}


Das funktioniert auch jedoch leider wird nur eine Zeile ausgegeben. Wie mache ich das am besten?

Danke im voraus
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1583

PostPosted: Sun May 29, 2016 4:59 pm    Post subject: Reply with quote

PHP:
Code:
$_cond = array();

foreach($conditions_today as $cond) {
    $_cond[] = array(
        'time' => $cond->getTime(),
        'temperature' => $cond->getTemperature(),
        'humidity' => $cond->getHumidity(),
        'text' => $cond->getSummary()
    );
}

$smarty->assign('conditions', $_cond);


Smarty:
Code:
{foreach $conditions as $condition}
<tr><td>{$condition.time|date_format:"H"}</td>
<td>{$condition.temperature} C</td>
<td>{$condition.humidity * 100}%</td>
<td>{$condition.text|escape}</td></tr>
{foreachelse}
<tr><td colspan="4">Seriously, what kind of weather do you expect after the end of times?</td></tr>
{/foreach}


(In short, you're doing it wrong.)
Back to top
View user's profile Send private message
omexlu
Smarty Rookie


Joined: 13 Mar 2014
Posts: 27

PostPosted: Mon May 30, 2016 7:50 am    Post subject: Reply with quote

Works thanks Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP