view template from an 3rd party API which has object and array mixed up

30 views
Skip to first unread message

karthick b

unread,
Aug 6, 2017, 5:09:19 PM8/6/17
to Fat-Free Framework
Hello F3ians,

Re: View template from an 3rd party API which has object and array mixed up

This is more of  PHP question  rather than a F3 specific one. first accept my apologies for that.

I have a 3rd party API for which I am writing a wrapper in F3.

Following is the controller part .

// $kite is 3rd party API object
$orders
= $Kite->orders();

print_r
($orders);

$f3
->set('order',$orders);

...

print_r of $orders outputs the following array and object spaghetti


Array
(
    [0] => stdClass Object
        (
            [average_price] => 0
            [cancelled_quantity] => 0
            [disclosed_quantity] => 0
            [exchange] => NSE
            [exchange_order_id] =>
            [exchange_timestamp] =>
            [filled_quantity] => 0
            [instrument_token] => 1
            [market_protection] => 0
            [order_id] => 170805000005437
            [order_timestamp] => 2017-08-05 17:41:37
            [order_type] => MARKET
            [parent_order_id] =>
            [pending_quantity] => 0
            [placed_by] => ZZ07
            [price] => 0
            [product] => NRML
            [quantity] => 1
            [status] => REJECTED
            [status_message] => error user/user_target not logged in
            [tag] =>
            [tradingsymbol] => BANKNIFTYAUG
            [transaction_type] => BUY
            [trigger_price] => 0
            [validity] => DAY
            [variety] => regular
        )

    [1] => stdClass Object
        (
            [average_price] => 0
            [cancelled_quantity] => 0
            [disclosed_quantity] => 0
            [exchange] => NSE
            [exchange_order_id] =>
            [exchange_timestamp] =>
            [filled_quantity] => 0
            [instrument_token] => 1
            [market_protection] => 0
            [order_id] => 170805000005573
            [order_timestamp] => 2017-08-05 18:30:42
            [order_type] => MARKET
            [parent_order_id] =>
            [pending_quantity] => 0
            [placed_by] => ZZ07
            [price] => 0
            [product] => NRML
            [quantity] => 1
            [status] => REJECTED
            [status_message] => error user/user_target not logged in
            [tag] =>
            [tradingsymbol] => INFY
            [transaction_type] => BUY
            [trigger_price] => 0
            [validity] => DAY
            [variety] => regular
        )
       
....
)


 I want to output the above data in the view template with the <repeat> markup like below
<repeat group="{{ @order }}" value="{{ @value }}">
   
<td>
       {{ @value.average_price }}
       
</td>
       
<td>
           {{ @value.tradingsymbol }}
       
</td>
</repeat>

Thanks in anticipation.





ved

unread,
Aug 6, 2017, 5:30:27 PM8/6/17
to Fat-Free Framework
You appear to have an array of objects so you'd probably have to use @value->average_price instead of @value.average_price etc inside your repeat block.

karthick b

unread,
Aug 7, 2017, 4:44:49 AM8/7/17
to Fat-Free Framework
@ved,

It worked like a charm. Never thought I could access objects like this in templates or I forgot it.

Thanks once again. Appreciated.

ved

unread,
Aug 7, 2017, 5:10:54 AM8/7/17
to Fat-Free Framework
No problem.

If you wish to access your properties using array notation and at some point you use json_decode() to decode your API responses, then you can pass "true" as second parameter to json_decode and that should convert the objects down to arrays.

Good luck.
Reply all
Reply to author
Forward
0 new messages