Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Pass data from .php code to html.twig code

Solved by Ole Vik View solution

Started by Luca Caputo 6 years ago · 12 replies · 2129 views
6 years ago

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:
<div class="container">
{% 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 %}
</div>

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
6 years ago Solution

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.

6 years ago

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

6 years ago

@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.

6 years ago

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
6 years ago

@lucapu88, Certainly not!

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

6 years ago

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

PHP
  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:

HTML
  {% 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

6 years ago

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
6 years ago

@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.

6 years ago

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?

last edited 07/06/20 by Luca Caputo

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 74 7 hours ago
General · by pamtbaau, 12 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 39 5 days ago