Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] In FormsService use $view->renderFile() instead of $view->renderString() for better debug output #1224

Closed
rob-baker-ar opened this issue Apr 22, 2024 · 3 comments
Assignees
Labels
freeform5 Affects Freeform 5.x issue Something isn't working correctly

Comments

@rob-baker-ar
Copy link

When an exception is thrown or an error occurs in a custom form rendering template, in devMode, exceptions that are displayed on the front-end are hard to debug because the original Twig template file is unknown to the view layer and therefore it cannot show the template code where the exception occurred. This is because the template is loaded as a sting instead of a file.

A fix to this would be to change this call for view->renderFile() (dropping the file_get_contents() wrapper around $templatePath of course):

$output = \Craft::$app->view->renderString(
file_get_contents($templatePath),
[
'form' => $form,
'formCss' => $this->getFormattingTemplateCss($templateName),
],
$templateMode,

This seems to work well here.

Steps to reproduce

  1. Custom form rendering template containing an error (i.e. reference an undefined variable or something)
  2. Have devMode switched on so "friendly" errors with traces are shown
  3. Try to render the form
  4. An exception trace is shown but with template names like __TwigTemplate_e733123153b2c97d929a8fefb509ae2a instead of the Twig source from the file.

Expected behavior

In the exception front-end view, show the line and Twig context.

Craft & Plugin Info (please complete the following information):

  • Craft Version: 4.8.9
  • Freeform Version: 5.1.x
  • Freeform Edition: Pro
  • Fresh Install or Upgrade: N/A
@rob-baker-ar rob-baker-ar added the issue Something isn't working correctly label Apr 22, 2024
@gustavs-gutmanis
Copy link
Contributor

Hi @rob-baker-ar,

We will update this part, and we'll see if there are other places which can be adjusted as well, great suggestion! Thank you.

@gustavs-gutmanis gustavs-gutmanis self-assigned this Apr 22, 2024
@rob-baker-ar
Copy link
Author

I should add that I have just been trying this out while working on my form template. I am getting issues with different contexts using renderFile(), but there are other options of course like renderTemplate() which would work but will require a template path relative to a template root rather than an absolute file path as is the case now.

@kjmartens
Copy link
Contributor

This is available now in Freeform 5.1.17+. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
freeform5 Affects Freeform 5.x issue Something isn't working correctly
Development

No branches or pull requests

3 participants