Rendering custom comments template with blade

I’m trying to use a custom template for comments by calling comments_template() with the path to my custom template file in content-single.blade.php

@php(comments_template('/resources/views/partials/comments-custom.blade.php')

At the moment the comments-custom.blade.php is simply a duplicate of comments.blade.php

The custom template file is found and included but not rendered by blade, instead outputting unprocessed text… @if (! post_password_required()) etc

is it possible to render a blade file as a custom comments template or am I going about this the wrong way?

Based on my naive reading of the related code: I don’t think what you’re trying will work in the way you expected it to.

Here’s what Acorn does with comment templates:

And here’s what WP does with comments_template():
https://github.com/WordPress/wordpress-develop/blob/781953641607c4d5b0743a6924af0e820fd54871/src/wp-includes/comment-template.php#L1407-L1621

I haven’t tested this out, but I think how this would work is that you could pass an argument to comments_template() which is a file that doesn’t exist, i.e. comments_template( 'custom-comments' ). Since that doesn’t exist, Acorn will load resources/views/partials/comments.blade.php which will then be included by comments_template(). Since it’s included, you should then have access to the $file variable (in this case containing 'custom-comments') in your comments.blade.php partial, and you could use a switch or similar to then load an appropriate other file.

Keep in mind I only woke up like an hour ago, though. I might be over-complicating this.

1 Like

Thanks @alwaysblank ! That works :+1:

1 Like