Manually render blade template in functions.php

I try to render a blade template file manually in a function in the functions.php but i’m not achieving it. I already read this topic: How to render a blade template from filter function but the suggested tips did not help in my case.

I tried something like this:
\App\template('ajax.form', ['param' => 'value']);

Basically I want to make an AJAX call from the frontend, then fill in some parameters into a blade template and return the rendered view.

Is this possible in a convenient way in Sage9 or is there another way to do this?

That should work as long as you’ve got your template file in resources/views/ajax/form.blade.php.

What is happening? Are you getting any error messages or just no output? Sometimes other problems with the AJAX process can make errors hard to spot.

If you can provide more details and code, it will be easier for others to understand what is going on and help you.

1 Like

Try this (depending where your ajax form is located):
\App\template('partials/ajax-form', array('param' => 'value'));

2 Likes

Ok. I do not know what has happened, but suddenly it works!

However, to provide some more infos on people trying to achieve the same thing:
I first got ‘Error 500’ with no output.

My current (working) setup looks like this:
The blade template is located in views/ajax/form.blade.php.
In my functions.php I echo the content like this:
echo \App\template('ajax.form', ['var' => $variable]);

It works as expected: I have the $var in my template available. I still can not understand why it suddenly works and neither can I reproduce the error, but I’ll keep you updated as soon as the problem reoccurs.

Glad it’s working :slight_smile:

With a 500 error, you should have more details in your web server’s error log… My guess is that there was a PHP error somewhere, possibly in your compiled Blade template (they get stored in your wp-content/uploads/cache folder). Maybe there was a delay in the compiled templates being updated.

I’ve got caching issues as well with blade. Sometimes there’s a delay of a few seconds before my changes are visible

That must be a bit frustrating… I haven’t encountered any issues with my setup (Laravel Valet) and I’m usually running yarn start with Browsersync so the page refreshes as soon as I update a Blade template.