Anahkiasen / flatten

A package to flatten any website to plain HTML
336 stars 42 forks source link

How to Cache Only a Part of My View #38

Closed rajivseelam closed 9 years ago

rajivseelam commented 9 years ago

My master layout is almost like following:

<html>
<head>

</head>
<body>
    <div id="wrapper" class="clearfix">

        @include(layouts.header')

        <?php Flatten::start() ?>
        @yield('content')
        <?php Flatten::end() ?>

        @include('layouts.footer')

    </div

</body>
</html>

But I only want to cache whatever is generated in @yield('content') part. Why because my header includes login and logout buttons, if they are also cached, even if user is logged in, he is shown "Login".

Is it possible to cache only this part of the page keeping rest of the page dynamic?

rajivseelam commented 9 years ago

And I keep on getting Undefined variable: __env whenever I try to use Runtime caching, caching in small sections.

vitalikaz commented 9 years ago

That's because some of blade commands are compiled to Laravel's $__env calls. For example @include is compiled to $__env->make() call. And the @cache and other Flatten's commands generate closures, so $__env is not accessable inside them. As for now, you can't pass variables you want to use inside that closure while using @cache command (I guess with start() too). But you can use Flatten's section() method and pass needed variables to closure:

<?php print Flatten::section('section-name', function() use ($__env) { ?>
Your content
<?php }); ?>
rajivseelam commented 9 years ago

Here, I am facing another problem.

For Flatten to work I have to enable flatten in config and set it to true (which makes flatten cache all page). In that case, what is the point of using a section.

It's totally possible that I am wrong here, because I don't entirely understand how to use Flatten from documentation.

rajivseelam commented 9 years ago

Now it is working.

Thanks.