基于if语句的laravel css样式

时间:2014-10-16 20:55:19

标签: php laravel laravel-4 blade

我试图找出如何将不同的CSS样式应用到我的index.blade.php页面,具体取决于$ category是否处于活动状态。

即如果网址中没有指定类别,则图片会很小,如果网址中有类别,则图片会很大。

Route.php

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

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

2 个答案:

答案 0 :(得分:0)

您可以更改:

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

成:

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

现在在Blade:

@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

答案 1 :(得分:0)

如果您的 css 是内部 css,那么您可以使用以下方法:

<style>

@if(your condition)

.container-wizard {

    width:100%;
    position: relative;
    margin: 0 auto;
  
    
}
@else

.container-wizard {

width:83%;
position: relative;
margin: 20 auto;


}


 @endif

</style>

如果您的 css 是外部的,您可以通过引用链接使用相同的方法:

@if(your condition)
  <link href="/assets/aos.css" rel="stylesheet">

@else

  <link href="/assets/vendor/style.css" rel="stylesheet">

@endif

此外,您可以为您的类别添加标志。