2014-10-16 1 views
0

Я пытаюсь выяснить, как я могу применять различные стили CSS для моей страницы index.blade.php в зависимости от того, активна ли категория $.laravel css styling на основе оператора if

I.e Если в URL-адресе нет категории, указанной в 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) –

+0

@tomharrison Был один ') 'отсутствует. Я исправил его –