Roots Discourse

Return blade template view with \App\template_path

#1

Hi,

I am trying to add a ContactForm7 custom form tag in Sage.

The form tag callback has to return some HTML and I wanted to place it in a blade template, but it’s not working. Here is the code I currently have:

add_action( 'wpcf7_init', function() {
    wpcf7_add_form_tag( 'challenges', 'custom_challenges_form_tag_handler' );
});

function custom_challenges_form_tag_handler( $tag ) {
  return \App\template_path(locate_template("views/partials/contact-challenges.blade.php"));
}

Instead of displaying the content of the blade file, it shows the path to the cached file:

/web/mysite/web/app/uploads/cache/3aff462176e85875262fda9c9fad74026a4162ec.php

Any idea how I can fix this?

0 Likes

#2

Hi @ierpe,

Instead of

  return \App\template_path(locate_template("views/partials/contact-challenges.blade.php"));

Try

  return \App\template('views/partials/contact-challenges');

Or

  return \App\template('partials.contact-challenges');
1 Like

#3

Thanks that solved it!

I tried with template but could not get it right.
I was using the following code with an include and that was working:

include \App\template_path(locate_template("views/partials/{$slug}.blade.php"));

but with a return or an echo… not.
Thanks for the precision.

Is there a documentation page somewhere on template and template_path? I could find any…

0 Likes

#4

Could you elaborate on

I tried with template but could not get it right.

@codepuncher’s suggestion should work.

\App\template_path() will return the path or your template file, but you can’t include blades because they are not valid PHP; they have to be compiled by the blade engine into valid PHP.

\App\template(string $blade, array $data) returns a string is HTML rendered from

  1. your blade file $blade
  2. the data passed to it $data

Your original function should look something like this:

function custom_challenges_form_tag_handler( $tag ) {
  return \App\template("partials.contact-challenges"));
}

(That assumed that your partial does not have any variables in it; if it does, you would need to pass a second argument to template()that is an associative array w/ the variables the blade needs.)

0 Likes

#5

Yes as I mentioned before @codepuncher’s solution worked.

I tried with template but with wrong paths and didn’t really realize the differences between the two function at first (even though I realize now the name is self-explanatory…)
I’m new to Sage, and it’s too bad there isn’t a proper documentation on all function available…
Anyway thanks for the replies people!

0 Likes