Is it possible to gradually migrate from plain php templates to blade?

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!

Hey @bruno,

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.

Thanks @mmirus!
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 page.php:

<?php get_header(); ?>

<div class="wrap">
	<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 -->

<?php get_footer();

You could copy page.blade.php from Sage:


  @while(have_posts()) @php(the_post())

And replace the inner part of it with the inner part of your page.php:


  @while(have_posts()) @php(the_post())
      // 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.

1 Like

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.

Thanks again!


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/@endphp and/or <?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 <?php and ?>.

Ugly though :wink:

1 Like