Laravel without Blade - 控制器和视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14010361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel without Blade - Controllers and Views
提问by James Wagoner
I am more efficient at setting up my view logic with straight up php. Blade is cool but it's not for me. I am trying to translate all the Blade specific examples and docs to just php. I don't like the fact that I need to assign all the variables for my views in an array of View::make(). I did found all of this so far.
我可以更有效地直接使用 php 设置我的视图逻辑。Blade 很酷,但不适合我。我正在尝试将所有 Blade 特定示例和文档翻译成 php。我不喜欢我需要在 View::make() 数组中为我的视图分配所有变量的事实。到目前为止,我确实找到了所有这些。
controllers/home.php:
控制器/home.php:
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->name = 'James';
$this->layout->nest('content', 'home.index');
}
}
views/layouts/default.php:
视图/布局/default.php:
// head code
<?php echo Section::yield('content') ?>
// footer code
views/home/index.php
意见/主页/index.php
<?php Section::start('content'); ?>
<?php echo $name ?>
<?php Section::stop(); ?>
I am greeted with this error: Error rendering view: [home.index] Undefined variable: name
. I know that $this->layout->nest('content', 'home.index', array('name' => 'James'));
works but that negates my point about having to send all my variables to an array. This can't be the only way.
我遇到了这个错误:Error rendering view: [home.index] Undefined variable: name
。我知道这$this->layout->nest('content', 'home.index', array('name' => 'James'));
行得通,但这否定了我必须将所有变量发送到数组的观点。这不可能是唯一的方法。
The view templating docs doesn't seem to touch on doing variables with nested views from controllers.
视图模板文档似乎没有涉及使用来自控制器的嵌套视图执行变量。
采纳答案by afarazit
Here's an example of how I'm templating with laravel.
这是我如何使用 laravel 进行模板化的示例。
Class Products_Controller extends Whatever_Controller {
public $layout = 'layouts.main';
public function get_index()
{
// .. snip ..
$view = View::make('home.product')
->with('product', $product); // passing all of my variable to the view
$this->layout->page_title = $cat_title . $product->title;
$this->layout->meta_desc = $product->description;
$this->layout->content = $view->render(); // notice the render()
}
}
my main layout looks like
我的主要布局看起来像
<html>
<head>
<title> {{ $page_title }} </title>
<meta name="description" content="{{ $meta_desc }}" />
</head>
<body>
{{ $content }}
</body>
</html>
and the home/product page looks like
主页/产品页面看起来像
<div class="whatev">
<h1> {{ $product->title }} </h1>
<p> {{ $product->description }} </p>
</div>
Hope that helps you clear some things up
希望能帮助你理清一些事情
回答by Laurence
you can pass variables this way;
您可以通过这种方式传递变量;
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->nest('content', 'home.index')
->with('name', 'James');
}
}
回答by Christopher Rathgeb
I know it has been a while on this question, but since it was asked, Laravel 4 has come out and there are newer ways to do things.
我知道这个问题已经有一段时间了,但是自从被问到之后,Laravel 4 已经问世,并且有更新的方法来做事。
If you are reading this these days you should consider using View Composers to prepare the data for your views.
如果您最近正在阅读本文,您应该考虑使用 View Composers 为您的视图准备数据。
Example:
例子:
class MyViewComposer {
public function compose($view){
$view->title = 'this is my title';
$view->name = 'joe';
...
$view->propertyX = ...;
}
}
After setting up your view composer register it with the app:
设置您的视图作曲家后,将其注册到应用程序:
View::composer('home.index', 'MyViewComposer');
For more information check out the laravel docs on view composers:
有关更多信息,请查看有关视图作曲家的 laravel 文档: