C
C
cosonicsq2020-06-20 13:50:44
Drupal
cosonicsq, 2020-06-20 13:50:44

How to write words to the left of the data?

I'm making a custom module in Drupal8. Now my data that I receive from the API is displayed like this:

18.21
1020
63


But I need the words to the left of the data:

Температура: 18
Давление: 1020    
Ветер: 63

My controller code:
<?php

namespace Drupal\weather\Controller;  
use Drupal\Core\Render\Markup;

class WeatherPage
{
  public function getWeather($city)
  {
    $response = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q='.$city.',&appid=32ae008b1c7259324aa50450687fabf5&units=metric');
    $jsn = json_decode($response);
    
    return [
      '#markup' => Markup::create(
                                   '<h1>' . $jsn->name . '</h1>' . 
                                   '<div>' . $jsn->main->temp . '</div>' .
                                   '<div>' . $jsn->main->pressure. '</div>' .
                                   '<div>' . $jsn->main->humidity. '</div>'
                                  )
    ];
      
  }    
}


How to implement this? And how to make the temperature displayed without commas? And now the temperature is displayed 18.21 instead of 18 in the field$jsn->main->temp

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xenonhammer, 2020-06-20
@cosonicsq

(
 '<h1>'  . $jsn->name . '</h1>' . 
 '<div>' .  'Температура:' . $jsn->main->temp . '</div>' .
 '<div>' . 'Давление:' . $jsn->main->pressure. '</div>' .
 '<div>' . 'Ветер:' . $jsn->main->humidity. '</div>'
  )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question