2014-10-16 95 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