2
votes

I have written a training application with each page/slide of the training workbook as a seperate blade template file named as "page1.blade.php", "page2.blade.php" and so on. Each of these files has content of the kind:

@extends('en/frontend/layouts/training_modulename')

{{-- Page title --}}
@section('title')
    Page Title
@parent
@stop

{{-- Page content --}}
@section('pageContent')
    <div class="pageContentContainer">
        <h2>Page Title</h2>
        ...
    </div>
@stop

This works really well when being viewed page by page within the browser. However I also wish to automatically compile all pages into a PDF document. This is being done via dompdf which works amazingly well when I pass each pages html to it manually. However I wish to condense the @section('pageContent') section of each page into one large section which extends a different layout for passing to dompdf.

Given the above context my question is this: Is there a method in Laravel's blade parser which would allow me to pass it a blade file and just get the rendered html from a particular section? The below pseudo-code demonstrates what I would like to be able to do.

$pages = array(...); // content of the directory
foreach ($pages as $page)
{
    $renderedPage = Blade::render($page);
    $title = $renderedPage->title;
    $pageContent = $renderedPage->pageContent;
}
1

1 Answers

3
votes

Instead of doing the normal return of view

return View::make('page');

You can instead store the view in a string

$view = View::make('page');

So then you can do your code something like this (not tested - but you get the idea):

$pages = array(...); // content of the directory
foreach ($pages as $page)
{
    $renderedPage[] = view::make($page);
}