0

I'm trying to display some dynamic data using Zendesk's API. Within the API data, there is some HTML as well.

I've basically made a simple plugin for OctoberCMS and I'm having no issues getting this data and printing it, but what prints is raw HTML rather than rendered HTML. My PHP experience is pretty basic, so I'm not sure if I need to do something from the API call in the plugin or if this is something I need to look at with Twig (can you print_r with Twig?).

In any case, Here's some code I've used on the plugin to Get the data:

function getApiData($api_url)
    {
        $response = $this->curlGET($api_url);
        return json_decode($response, true);
    }

function curlGET($url)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
        ));

        $result = curl_exec($curl);
        curl_close($curl);

        return $result;
    }

And here's what it looks like on the page template:

{% if apiData != 'error' %}
  <div class="posts col-12 row">
    {% for article in apiData.articles %}
      <div class="post col-12 col-lg-6">
        <a href="{{ article["html_url"] }}" target="_blank" rel="noopener">
          <div class="post--content">
            <div class="post--date">{{ post["updated_at"] }}</div>
            <h3>{{ article["title"] }}</h3>
            <div>{{ article["body"] }}</div>
          </div>
        </a>
      </div>
    {% endfor %}
   </div>
{% endif %}

Just wanted to add, article["body"] contains HTML, so what I've done is create a new PHP array within the getApiData function:

function getApiData($api_url)
    {
        $response = $this->curlGET($api_url);
        $json = json_decode($response, true);
        $articleArray = array();
        foreach ($json['articles'] as $article) {
            $articleArray[] = array(
                "title" => $article['title'],
                "url" => $article['html_url'],
                "body" => $article['body']
            );
        }
        return $articleArray;
    }

I've tried print_r($article['body']) or var_dump($article['body']) but just prints $article['body'] as I want it to display, but at the top of the page rather than where I want it to be (within the div). How do I convert HTML from JSON to display as HTML normally would?

jimjamjosh
  • 15
  • 5
  • Does this answer your question? [How to display string that contains HTML in twig template?](https://stackoverflow.com/questions/8355123/how-to-display-string-that-contains-html-in-twig-template) – DarkBee Oct 01 '20 at 11:58
  • I want to do the opposite as what currently outputs is just raw HTML. To clarify, I want `

    Text

    ` to print as `Text` and not `

    Text

    `. Basically like var_dump or print_r, but not to the top of the page.
    – jimjamjosh Oct 01 '20 at 12:51
  • ... That is exactly what the filter `raw` does. Twig auto-escapes `html` in variables – DarkBee Oct 01 '20 at 12:54
  • I did try this earlier but broke the whole thing and couldn't figure out why. Somehow, now that I've updated one of the functions (see OP) it seems to have fixed it, but yes. You are correct. Thank you! – jimjamjosh Oct 01 '20 at 13:05
  • No problem glad you got it sorted out – DarkBee Oct 01 '20 at 13:06

0 Answers0