0

What is the best way to "send" a PHP variable between templates? I have an array $myArray that I create in my index.html template. Then I have a template which goes to fetch data from an external API, and store the items in $myArray. And then I've turned on caching for this template, so I won't have to churn through the API request on every load. Then back at the index.html I want to output stuff from $myArray.

Basically it would look something like this:

//index.html:
<?php global $myArray; ?>
{embed="include/api"}
<?php
foreach ($myArray as $m) {
    //output
} ?>

//api.html:
global $myArray;
$myArray = getAPIdata(); //getAPIdata just fetches stuff and returns the array

Now, this doesn't work of course. But I don't know why. And I don't have any other ideas on how to accomplish this. Especially since I want caching on my api.html template.

1 Answer 1

1

Don't ever enable php in a template. Aside from being a security risk it's also just ugly and hard to maintain.

Take some time to learn how to build your functionality as an add-on. Plugins are the simplest type of add-on to get started with: https://ellislab.com/expressionengine/user-guide/development/index.html

In terms of the specifics of saving data across page loads, take a look at the session class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.