I have a website build with sage 9 alpha 3 but build without laravel, just plain php.
At that time I had a tight schedule and could not spend any time digging into laravel. Fast forward today, I have to make some major changes in the project and wanted to upgrade the stack to latest.
My question is: Is it possible to run both blade and plain php templates side by side while I progressively migrate the code base?
Thanks in advance for any input!
Yes, I think that should be possible. Any files you put into the
views folder that are just
.php and not
.blade.php should be processed as normal PHP. I haven’t tried it, so I’m not sure what gotchas there might be.
I will deploy a new branch of my site where I’ll try this out.
I hope to post the result here soon.
Sounds good–I’m sure there will be someone else who will find that helpful.
Another possible approach would be that instead of having some
.php files and some
.blade.php files, you could try copying the appropriate Blade templates from Sage to your theme and replacing the relevant parts with your PHP code inside of
@php blocks, then refactoring those blocks over time. Here’s an example.
If your theme had this
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
while ( have_posts() ) : the_post();
// my custom page template stuff
endwhile; // End of the loop.
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
You could copy
page.blade.php from Sage:
And replace the inner part of it with the inner part of your
// my custom page template stuff
Basically, this would get it into Blade and using the
app.blade.php layout to handle all the wrapper stuff, and then as you go you can refactor that inner part to use Blade directives instead of PHP, prepare your data in the data filters or controller, etc.
Again, not sure what snags you might hit. Might take a little more cleaning up front, but would help keep things more consistent vs having both PHP and Blade templates.
Any plain PHP in a .blade.php file will render just fine, right? No need for separate .php and blade.php files. Just write normal php in your blade.php files.
Yes, if you place it inside the
@php directive as shown above. That’s fine as a stepping stone on the path from plain PHP templates to Blade templates, but as a best practice should be kept to a minimum. If you have more than a very small amount of code in
@php directives in your templates, it’s probably a sign you would benefit from refactoring that code into your controller or elsewhere.
Getting back just to say that the first solution provided by @mmirus works perfectly for me. I chose to move all my .php files to the views folder and I’m converting them one by one to view/controller pairs as I go along. The migration is being very straight forward.
Glad to hear it’s working out for you! Thanks for following up.
You don’t even need to use an
@php directive in Blade.
You can have a
*.blade.php file that opens with
<?php and closes with
?> and contains nothing but regular php and it will process just fine. From what I’ve read there’s not even a performance cost.
The only limit I’ve found it you can’t mix
@php( [some php here] ) with any
<?php/?> tags in the same file. But
@php( [some php here] ) appears to be depreciated anyway.
Or am I mistaken?
This is correct.
.blade.php will work with