Shortcode in wordpress to use blade partial in html

Is it possible to do something like …

function blade_func(){
    return view('partials.newsletter-block')->render();
}
add_shortcode( 'blade', 'blade_func' );

… so i can use [blade] in my html, which will be replaced?

Sure, your theme can register shortcodes and render them using blade templates.
The rendering is a bit different for Sage 9 and 10, what Sage is your theme based on?

I have “roots/sage-lib”: “~9.0.5” … I used the code in a new plugins function.php file, but got an error. So I try to find out, where to place the code.

First register a shortcode in WordPress:
https://codex.wordpress.org/Shortcode_API

In the shortcode render function you then render the blade template (Sage 9):
https://discourse.roots.io/t/manually-render-blade-template-in-functions-php/11292/3?u=strarsis

1 Like

Wow… it works. Thx @starise

Here is the final code:

function blade_func(){
    return \App\template('partials.newsletter-block', array('param' => 'value'));
}
add_shortcode( 'blade', 'blade_func' );
2 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.