• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Array from callback to template

localhost8080

Well-known member
#1
Hello, dear friends of this nice software.

Something is getting really lame here with v1.4:

I need to receive data from a callback function.

Within my method, I fill an array with data. before returning my array, I write it into my logfile to get sure the data gets populated.

Output in my logfile:

Code:
2014-12-27 18:22:03 ADDIDTIONALDATA: Array
(
    [key_1] => 01.01.2015
    [key_2] => error_successful
    [key_3] => bugware
)
Then, in my template, on

Code:
{xen:helper dump, {$additional_data}}
I receive

Code:
string(23) "
Array
"
looks like my brave array has been casted to a string? wtf!

and I can't access data with

Code:
{$additional_data.key_1}
which has been defined earlier:

Code:
<xen:set var="$additional_data">
                           <xen:callback class="Bugware_TemplateTools_ThreadHelper" method="getAdditionalData" params="{xen:array 'custom_fields={$thread.custom_fields}'}" />
                        </xen:set>

Any ideas? I'm really sick of wasting time with absolutely basic functions like array output within a template :censored:

Thank you.
 
Last edited:

Brogan

XenForo moderator
Staff member
#2
How have you made the array param available to the template?

It might be better if you post the relevant segment of PHP code.
 

localhost8080

Well-known member
#3
Hello Brogan,

here is the code:

Code:
public static function getAdditionalData(){
    $params = func_get_arg(1);
    $custom_fields = unserialize($params['custom_fields']);

    $additional_data = array();

    if(array_key_exists('key_1', $custom_fields)){
        $additional_data['key_1'] = $custom_fields['key_1'];
    }

    if(array_key_exists('key_2', $custom_fields)){
        $additional_data['key_2'] = $custom_fields['key_2'];
    }

    if(array_key_exists('key_3', $custom_fields)){
        $additional_data['key_3'] = $custom_fields['key_3'];
    }

    if(array_key_exists('key_7', $custom_fields)){
        $additional_data['key_7'] = $custom_fields['key_7'];
    }

    XenForo_Helper_File::log('bugware', 'ADDIDTIONALDATA: ' . print_r($additional_data, true), true);

    return $additional_data;
}
Thank you.
 

localhost8080

Well-known member
#8
S

Syndol

Guest
#9
Looks like you will have to do things a little differently in order to achieve what you want.
I'm not sure what your end result is within the template but why not use the PHP function to save your custom fields and then return HTML created using your array values.
 

Chris D

XenForo developer
Staff member
#12
Bear in mind a callback tag can return a template with view parameters which support arrays, e.g.

PHP:
        $viewParams = array(
            'additional_data' => $additional_data
        );

        return $template->create('your_template_name', $viewParams);
That might help.