Junior
Junior

Reputation: 11990

How to create a variable in a blade template in laravel 5 to be used by a partial view

I created a partial view which have a common form code. What I want to do it to change the value of the button, so I thought I would just create a variable in the parent view and just before @include the partial view I would set a variable which then the partial view use to display the correct title.

Here is my partial view code

  <div class="form-group">
    {!! Form::label('account_name_id', 'Account Name', ['class' => 'col-sm-2 control-label' ]) !!}
    <div class="col-sm-10">
      {!! Form::text('account_name', null, ['class' => 'form-control', 'id' => 'account_name_id', 'placeholder' => 'Account Name...']  ) !!}
    </div>
  </div>



  <div class="form-group">
    {!! Form::label('legal_name_id', 'Legal Name', ['class' => 'col-sm-2 control-label' ]) !!}
    <div class="col-sm-10">
      {!! Form::text('legal_name', null, ['class' => 'form-control', 'id' => 'legal_name_id', 'placeholder' => 'Legal Name...']) !!}
    </div>
  </div>



  <div class="form-group">
    {!! Form::label('company_code_id', 'Company ID', ['class' => 'col-sm-2 control-label' ]) !!}
    <div class="col-sm-10">
      {!! Form::text('company_code', null, ['class' => 'form-control', 'id' => 'company_code_id', 'placeholder' => 'Company Identifier...']  ) !!}
    </div>
  </div>



  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      {!! Form::submit($submitButtonTitle, ['class' => 'btn btn-primary']) !!} 
    </div>
  </div>

please note the $submitButtonTitle variable in my partial view above.

Then in the parent view I have done something like this

@extends('layouts.master')
@section('main')

    {!! Form::open(['route' => ['account_store_path'], 'class' => 'form-horizontal']) !!}

  {{ $submitButtonTitle = 'Add Account' }}
  @include ('accounts._form')

    {!! Form::close() !!}
@stop

The problem that I am having is I am not sure how to set variable without displaying it on the screen.

when I do

  {{ $submitButtonTitle = 'Add Account' }}

it actually sets the variable correctly but it also echo it to the screen. How can I only set the variable without echoing it to the screen?

Upvotes: 0

Views: 1201

Answers (2)

Franco
Franco

Reputation: 2329

This is a proper blade inclusion. You can add the partial to your view in this way:
@include('your_partial', ['submitButtonTitle' => 'Add Account'])

Upvotes: 1

user4621032
user4621032

Reputation: 641

use simple php tags

<?php $submitButtonTitle = 'Add Account' ?>

Upvotes: 1

Related Questions