How to pass data from sources to template function?

Started by asprin, September 23, 2019, 02:15:19 AM

Previous topic - Next topic

asprin

If I make a SELECT query from the sources file (sources/Example.php), then how would I pass the returned data to the template function being called?

sources/Example.php
function DoSomething()
{
    global $context;
    loadTemplate('Example');
    $data = <get some data by querying the database>
    .
    .
    .
    .
    .

    $context['sub_template'] = 'listData';

    // now pass this $data to the listData function
}


themes/default/Example.template.php
function template_listData()
{
    // access $data here
}


One thing that I can think of is creating a key in the $context array and assigning the data to that key and then accessing it via the global $context variable. But I was wondering if there is a better/alternate approach to this.

Arantor

That's exactly what SMF does, it's even why it's called $context: it contains everything contextually relevant to the current page.

asprin

That's great. I was just wondering if there was any alternative way of doing this too.

Arantor


Advertisement: