Laravel 4 - Including a "Partial" View Within a View (Without Using Blade Template)

call to Partial View in a blade that is already extending the layout: Laravel 5.2.37

To include one blade file into another, just use @include

@include('path.after.view.folder')

Documentation for Blade control structures

How to include the same partial view more than once with Blade?

You can use Blade Partial views to make Widgets.
However, you should not load the .css and .js files within a Widget Partial View because, like you said, it will load the files more than once.

If you find yourself loading .css and .js files depending on the pages, you can add to your main-layout.blade.php (the file where you have your tag something like this:

File: main-layout.blade.php

<!DOCTYPE html>
<html>
<head>
@if(isset($loadOnHead)) <!-- Check if $loadOnHead is defined to avoid errors -->
@foreach($loadOnHead as $file)
{{ $file }} <!-- Output all the files on the array -->
@endforeach
@endif
</head>
<body>

<!-- * Your Site/App Content * -->

<!-- Call the widget-css-js.blade.php file,
note that the contents won't be loaded here.
-->
<!-- * Your Widget * -->
@yield('widget')

<!-- * Your Widget Again * -->
@yield('widget')

<footer>
<!-- You can do another @foreach here for a $loadOnFooter
array if you want to load scripts on footer
(you should load .js on footer).
-->
</footer>
</body>
</html>

|

File: widget.blade.php

<!-- Your Widget HTML and Code goes here -->
* Your Widget Content *

|

Controller: at your Controller, when you build the page with the widgets (NOT the Widgets itself), you should:

public function MakeWidgetPage()
{
//Add Scripts and CSS files to the Head
$loadOnHead = array(

HTML::script('js/app.min.js'),
// Generates: <script src="http://your-app/js/app.min.js"></script>

HTML::style('css/style.css'),
// Generates: <link media="all" type="text/css" rel="stylesheet" href="http://your-app/css/style.css">

);

//Response
return View::make('page-with-widgets')
->with('loadOnHead', $loadOnHead);
}

Reference View Partial in Package using Blade?

I was able to solve it eventually. It seems there was an odd disparity of composer not having being updated, and me trying different prefixes. The final solution is:

@include('package-name::view.file')

Don't include vendor name, or namespace, or anything else (I was getting desperate).



Related Topics



Leave a reply



Submit