Pass data from .php code to html.twig code

I have a .php file with a converted API inside from which I go to get the data, the code is this:
<?php $api_data = json_decode(file_get_contents('https://prova1api.herokuapp.com/RedHotCHiliPeppersDiscography'), true);?>

then I have an .html.twig file with this *code:


{% for row in api_data %}


01



{{row.album}}


{{row.anno}}


author



{% endfor %}

how do I get them to communicate?
if I use: {% include ‘twig / floatingAPI.php’ with {‘truncate’: truncate}%}
it doesn’t work, it gives me an error

  • sorry, I don’t know why it doesn’t display the divs

You’ll need to load the data in a plugin, and pass it to Twig from there. Eg., in the onPageContentProcessed-event, use $this->grav['twig']->twig_vars['api_data'] = $api_data. It will then be accessible from Twig as you expect.

Many plugins do this, here’s an example in the Scholar-theme.

I’m using your own theme if I’m not mistaken, it’s called project-space.
what should I do? which plugin to download?
I’m sorry but I’m new in grav, plus I’m Italian, and I struggle with English

@lucapu88, There is no ready to use plugin for this. You’ll have to create your own plugin. See chapter 4. Plugins in the documentation.

In short:

  • Install plugin ‘devtools’ if you haven’t already:
    $ bin/gpm install devtools
  • Create a new plugin:
    $ bin/plugin devtools new-plugin
    • Answer the questions
    • Make sure to run composer update to initialize the autoloader.
      This should be run inside the folder of the plugin.
    • You now have a working plugin

Inside your plugin you will call the API to fetch the data from the site. The array created from the json, can then be passed to Twig as @OleVik explained.

In a theme inherited from theme ‘Project Space’, you can adapt an existing Twig template or create your own template and use a for-loop to run though the items.

For a step-by-step example, see Plugin Tutorial.

but olevik said they exist!

As pamtbaau explained, you’ll need to create the plugin. My code was an example of what you need to use in the plugin to pass the data to Twig.

1 Like

can we feel private? I need a hand please

@lucapu88, Certainly not!

But any technical question regarding Grav is allowed on the forum… This is a learning space for all of us.

I created a plugin because I have to call an API from PHP and print its contents in an .html.twig file

  public function callAPI() { //qui c'è la mia chiamata API
    $api_data = json_decode(file_get_contents('https://prova1api.herokuapp.com/RedHotCHiliPeppersDiscography'), true);
    $this->grav['twig']->twig_vars['api_data'] = $api_data;
    return $api_data;
}

this is the PHP code for the API (I don’t know if it is correct) but it doesn’t work because if in the .html.twig file I write:

  {% for row in api_data %}
<div class="singlePostContainer">
    <span class="postNumber">
        01
    </span>
    <div class="postTextContainer">
        <div class="postTitle">
            {{row.album}}
        </div>
        <div class="postDate">
            {{row.anno}}
        </div>
        <div class="postAuthor">
            author
        </div>
    </div>
</div>
{% endfor %}

it does not print anything. I hope I have been clear, in case tell me if you need more information. thank you

How are you using the callAPI()-method? Which event is it hooked into? As mentioned, the onPageContentProcessed-event should be late enough to have access to Twig to pass in the data.

You’ll want to use dump($api_data) in PHP and {{ dump(api_data) }} in Twig to verify that the data is retrieved, and that it is passed to Twig. The callAPI()-method also does not need to return anything, as it works as a setter.

1 Like

@lucapu88, I appreciate you are just introduced to programming and have limited experience.

The forum however is not the place to teach how Grav, Plugins and the event-system work.

I could provide you a ready to use plugin, but that will not teach you anything. I would therefor like to encourage you to read all sections of Chapter 4. Plugins. Please also follow the tutorial to get some hands-on.

If you encounter difficulties reading the chapter on plugins, you may ask for clarification by emailing me directly to not pollute the forum.

sorry but in the previous comments I asked you if we could feel in private, you said no, and that it was better to hear us on the forum, now you are telling me that I must not pollute the forum and I must write you in private … I don’t understand you !
I saw the plugin section, I started creating my plugin, it doesn’t work! the documentation is poor and not clear enough … I’m a newer ok, but I just can’t understand it! I built sites with Laravel, created APIs with express, did many things, and I never had problems following the documentation or watching video tutorials! grav is described as a very simple CMS to use even for beginners, but it is not. Apart from this forum, I find nothing else that can help me, since there is nothing on the internet besides this! neither on stack overflow, nor on youtube, nor on any other existing site!
And on this official forum, you tell me that you can’t teach grav … then explain to me what it is for!
Unfortunately I am forced to use this CMS for work, I did not choose it, so I have to do it … can you help me or not?

thank you very much! now it seems to work!