简体   繁体   English

如何在Laravel 5的刀片模板中创建变量以供部分视图使用

[英]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. 我想做的就是更改按钮的值,所以我想我只想在父视图中创建一个变量,就在@include部分视图之前,我将设置一个变量,然后该部分视图用于显示正确的变量标题。

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. 请注意我上面的局部视图中的$submitButtonTitle变量。

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? 如何只设置变量而不回显屏幕?

使用简单的PHP标签

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

This is a proper blade inclusion. 这是正确的刀片夹杂物。 You can add the partial to your view in this way: 您可以通过以下方式将局部视图添加到视图中:
@include('your_partial', ['submitButtonTitle' => 'Add Account']) @include('your_partial',['submitButtonTitle'=>'添加帐户'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM