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

Discussion in 'XenForo Development Discussions' started by localhost8080, Dec 27, 2014.

  1. localhost8080

    localhost8080 Well-Known Member

    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: Dec 27, 2014
  2. Brogan

    Brogan XenForo Moderator Staff Member

    How have you made the array param available to the template?

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

    localhost8080 Well-Known Member

    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.
     
  4. localhost8080

    localhost8080 Well-Known Member

    so what about my nice array now? :cry:
     
  5. localhost8080

    localhost8080 Well-Known Member

    simple questions, big problems. by searching through the great documentation I fond this

    https://xenforo.com/community/threads/xenforo-template-syntax.23245/

    so how do I set "compley" veriables like arrays?
     
  6. localhost8080

    localhost8080 Well-Known Member

  7. Syndol

    Syndol Guest

  8. localhost8080

    localhost8080 Well-Known Member

    I have this one, as I get an array from my callback method. but it doesnt work...

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

    Syndol Guest

    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.
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    What do you use $additional_data for after you have set it? Might be relevant.
     
  11. localhost8080

    localhost8080 Well-Known Member

    {xen:helper dump, {$additional_data}}

    I receive an array and I want to check if keys are set to display some values from additional data...
     
  12. Chris D

    Chris D XenForo Developer Staff Member

    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.
     

Share This Page