2014-10-16 109 views
0

我想弄清楚如何将不同的CSS样式应用于我的index.blade.php页面,具体取决于$ category是否处于活动状态。基于if语句的laravel css样式

I.e如果没有在url中指定的类别,那么图像将会很小,如果url中有一个类别,图像将会很大。

Route.php

Route::get('blog/{category}', ['as' => 'blog.category', 'uses' => '[email protected]']); 

blogcontroller.php

public function getCategory($category = null) 
{ 
    // get all the blog stuff from database 
    // if a category was passed, use that 
    // if no category, get all posts 
    if ($category) 
     $posts = Post::where(function($query) use($category){ 
        $query->where('category','=', $category); 
      }) ->get(); 

    else 
     $posts = Post::all(); 



    // show the view with blog posts (app/views/blog.blade.php) 
    return View::make('blog.index') 
     ->with('posts', $posts); 
} 
} 

index.blade.php

@foreach ($posts as $post) 

<h2>{{ $post->id }}</h2> 
<h2>{{ $post->img }}</h2> 
<p>{{ $post->name }}</p> 
<p>{{ $post->category }}</p> 
@endforeach 

回答

0

您可以更改:

return View::make('blog.index') 
     ->with('posts', $posts); 

到:

return View::make('blog.index') 
     ->with('posts', $posts) 
     ->with('category', $category); 
在刀片

现在:

@foreach ($posts as $post) 
<h2>{{ $post->id }}</h2> 
<h2> 
@if (is_null($category)) 
{{ $post->img }} - here somehow you need to display small image using image width/height or thumbnail 
@else 
{{ $post->img }} 
@endif 
</h2> 
<p>{{ $post->name }}</p> 
<p>{{ $post->category }}</p> 
@endforeach 
+0

谢谢,我已经试过,但有语法错误,意想不到的 ':',期待 '('。 (is_null($ category) – 2014-10-17 17:40:12

+0

@tomharrison缺少一个'')。我纠正了它 – 2014-10-17 18:17:33