1

I am facing a pretty strange problem working with templates in Laravel. I will demonstrate it with the following example.

css/custom.css

.muted{ color:red }

Layout.blade.php

<html>
  <head>
    <title> HomePage </title>
    <link href="css/custom.css" rel="stylesheet" />
  </head>
  <body>
      <p>Layout file</p>
      @yield('content') 
  </body>
</html>

subview1.blade.php

@extends('Layout')

@section('content')
   <p class="muted"> From sub view 1 </p>
@stop

subview2.blade.php

@extends('Layout')

@section('content')
     <p class="muted"> From subview 2 </p>
@stop

Actually what is happening is that for subview 2 the class muted is not applied. And console shows that it could not load the resource i.e custom/css when second view is loaded but works fine with subview1

Any pointers to the real issue and solution are highly appreciated.

EDIT

Attached is the directory structure. And I am referencing it like this.

1) dashboard_layout.blade.php (Layout File)

2) dashboard_index.blade.php ( subview1 )

3) course_index.blade.php (subview2)

dashboard_index.blade.php

@extends('teacher.dashboard.dashboard_layout')

course_index.blade.php

@extends('teacher.dashboard.dashboard_layout')

blade_stack_ques_ref

Nauman Zafar
  • 1,083
  • 15
  • 40
  • Your subviews and layout are in the same directory? – jeanj Jul 21 '16 at 14:06
  • Thanks Vuldo for your time. Actually, subview1 and layout are in same directory. and subview2 in other directory. but I am referencing them right. – Nauman Zafar Jul 21 '16 at 14:13
  • Can you please tell me exactly where your file are located on each others? – jeanj Jul 21 '16 at 14:19
  • @Vuldo I've edited the post. Please check them – Nauman Zafar Jul 21 '16 at 14:40
  • The problem doesn't come from here, you are referencing the right, I think the problem come from how the css is loaded, can you show me the `href`. Since in the second subview the css won't be called from the same path – jeanj Jul 21 '16 at 14:47
  • Vuldo, both the subviews share a layout and css is referenced in that shared layout.So In my opinion that won't be an issue. What do you say. – Nauman Zafar Jul 21 '16 at 14:50

1 Answers1

0

Actually, It's always confusing to reference the same assets using relative paths.You have to have an eagle eye to work out.Here comes Laravel's Html providers. Use them to reference your assets in your blade files as mentioned below rather than using simple link and script tags.

Html::style('assets/css/custom.css') // For style sheets.
Html::script('assets/js/custom.js') // For scritps.

NOTE : assets directory mentioned above is under your public folder as per Laravels app structure.

Html & Form Providers were shipped with Laravel basic app in previous versions.But in latest versions of Laravel. They are removed from the core.So above code snippet will produce an error. You can follow the minimal steps mentioned here laravelcollective to fix the issue.

Happy Learning !

Nauman Zafar
  • 1,083
  • 15
  • 40