如何在多个视图中使用变量值Laravel

时间:2014-04-23 08:28:11

标签: php symfony laravel laravel-4

控制器::

$states=$this->States->get_States();
$data=array('states'=>$states);
echo  View::make('frontend.list-property')->with('data', $data);

list-property.blade.php :: //这是子视图

@extends('layouts.frontend.frontend_login') 

@section('content')
<select>
  {{$states_drpdown}} //when I use this statement I am getting error

 </select>
 @stop

frontend_login.blade.php //这是布局文件

   <?php
    $states_drpdown='';
    foreach ($data['states'] as $state):
    $states_drpdown.='<option value="'.$state->sid.'">'.$state->statename.'</option>';
endforeach
   ?>
    <select>
     {{$states_drpdown}}  //I am getting list of options here
    </select>
    @yield('content')

我收到了以下错误,任何人都可以帮我解决。

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Method Illuminate\View\View::__toString() must not throw an exception

2 个答案:

答案 0 :(得分:2)

您还可以在所有视图中共享一段数据:

View::share('name', 'Steve');

您可以找到有关此here的更多信息。

在您的情况下,请尝试:(警告未经测试的代码

$states = $this->States->get_States();
$data   = View::share('states', $states);
return View::make('frontend.list-property');

答案 1 :(得分:1)

$data = array('states' => $this->States->get_States());
echo View::make('frontend.list-property', $data);

假设$this->States->get_States()返回一串选项,而不是数组?

<select>
    {{$states}}
</select>

如果$this->States->get_States()返回一个数组,则可以执行此操作(不需要<select>标记):

{{ Input::select('name-of-field', $states, Input::old('name-of-field')) }}

考虑返回视图而不是回显。