Reputation: 35
I am learning Laravel (and php) and creating views in blade. Because I'll have a number of very similar views, I want to keep as much of the formatting in reusable files that I pass a few variables to to customize for each file. This is working well for most instances, but fails when I need to pass a function to the included view.
show.blade
@include('_twocol_row_content',
[
'field' => 'Content',
'value' => $entry->blocks,
'interface' => blockDisplay()
])
_twocol_row_content.blade
@extends('_twoCol_row_base')
@section('field')
{{ $field }}
@overwrite
@section('value')
@foreach ($value as $instance)
<p>{{ $instance->$interface }}</p>
@endforeach
@overwrite
This works when the $interface has a direct relationship and can be passed as a string (e.g. 'title'), but fails with a "call to undefined function error" when a function is passed, because the function is only defined AFTER the foreach loop has started. I've tried everything I can think of and have search around the internet, but haven't found a way to make it work. If I put the whole for each loop directly in the view the content renders correctly, but I loose the benefit of all the formatting that is part of the included file.
I've tried:
{{ $interface }}
and passing the $instance variable as $instance->blockDisplay() fails because instance is not defined. Wrapping $instance in an isset renders the page but not the content.How can I render any custom functions without loosing the benefit of defining the wrapping html in a single file?
Upvotes: 1
Views: 1580
Reputation: 589
u can try this
to call object/collection value using this : $variable->{$field}
or object/collection that have method : $variable->{method}()
Upvotes: 1