Is it possible to use blade templates outside of returning a view?
I have found that there are some instances where I need a method to return a lengthy bit of HTML. Such as returning a formatted HTML representation of an object. I was wondering if there is nicer way of doing this by leveraging the Blade syntax to put together a kind of partial template.
Simply use the "@include()" function.
Blade templating will automatically know you need to include another blade template so, in this example, it'd include "resources/views/partials/navigation.blade.php".
<DOCTYPE html>
<html>
<head>
<!-- Head content -->
</head>
<body>
<!-- Include nav -->
@include('partials.navigation')
</body>
</html>
I am using a presenter to display a model in HTML. The presenter class has a method which I can invoke like $foo->present()->asHtml()from my views however the asHtml() is rather lengthy and it would be much more concise with blade however the only way I know to use blade is to return it as a view which of course I do not want because that would return just the partial and not the entire page that I want.
Sorry, I am not really sure if I am doing a good job of explaining the issue or what I what I want, please let me know if there is any part that needs further clarification. Thanks!
As mentioned before, you could include partials in your main template where you do $foo->present()->asHtml() and pass in the data required for rendering, or may be view composers if that suits you ??
Yes you can do as you suggest, despite the other comments which are still valid. I use blade to construct xml files and of course blade can be used to construct HTML email.