jumki
jumki

Reputation: 35

Laravel Blade, Pass a function through @include without checking if it exists in main view

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:

How can I render any custom functions without loosing the benefit of defining the wrapping html in a single file?

Upvotes: 1

Views: 1580

Answers (1)

Alzafan Christian
Alzafan Christian

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

Related Questions