October: Output functions do not work when calling a method from a component

Created on 4 Jun 2019  路  19Comments  路  Source: octobercms/october

  • OctoberCMS Build: v1.0.455
  • PHP Version: 7.3.6
  • twig/twig: v2.11.1

Description:

When calling any component method from any page / template - it is impossible to output information through output functions (echo, print_r, dd, etc.)

Steps To Reproduce:

  1. Create and register component
  2. Create a page and plug in the previously created component.
  3. In the component, create any function.
  4. Access this function through a page / template (example: {{ Component.yourMethod() }})
  5. In this function, call the output function (echo, print_r, dd, etc.)
  6. You can then put die() and make sure that the answer is empty or returned with an error in the case of dd()

As I understand it because of the update in twig, here's a link to the change, if you return ob_start(); - the problem disappears

Medium Completed Bug third party

Most helpful comment

All 19 comments

@jexme could you provide an example of how the behaviour is supposed to work and explain the difference between when it was working vs now in a PR to the test plugin?

I have the same problem, when I call a function with dd() from a template, then the server gives 500 error instead of a page with debug data like this.
image

Best for now is to downgrade twig to 2.10 looks like issue in twig. Caused with this PR: https://github.com/twigphp/Twig/pull/2995

Best for now is to downgrade twig to 2.10 looks like issue in twig. Caused with this PR: twigphp/Twig#2995

At 2.10 the problem is reproduced, at 2.9 is ok.

@jexme could you provide an example of how the behaviour is supposed to work and explain the difference between when it was working vs now in a PR to the test plugin?

created pr: https://github.com/octoberrain/test-plugin/pull/72

@LukeTowers, @Nai4rus, @Samuell1 - can confirm, twig messed up in this PR: https://github.com/twigphp/Twig/pull/2995

They removed one crucial part from ob_start -> the buffer.

... When output_callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser....

https://php.net/manual/en/function.ob-start.php

An easy fix would be to return the buffer as it is: ob_start(function($in){return $in;});, but still should be done in Twig project.

@w20k it looks like Twig doesn't want to make the fix, should we just fix it ourselves in our own code?

@LukeTowers @w20k They're making a PR for it here: https://github.com/twigphp/Twig/pull/3059 - Twig has to be in debug mode though. Not sure if that's tied to our debug mode at all.

@bennothommo I'm pretty sure it is, so I don't think that will be acceptable for us.

For our part, we can make initialize twig with debug enabled, if debug is enabled in config/app.php

@bennothommo I believe it should work! It might cause other issues because of the fix, but not 100% sure.
@LukeTowers three options that we have right now:

  1. Freeze twig version to v2.9.x
  2. Fix it in a fork (revert PR #3059)
  3. Test their new PR and hope it will work without any additional issues

@LukeTowers I can understand the reasoning for their change - they are trying to prevent leaking the template code if there's an Error or Exception half way through rendering the code. People shouldn't be using dd() or print_r() in production in an ideal world, anyways. Nonetheless - when I tested it, I didn't even get October's nice error page when it got to the dd(), so if we kept the behaviour as is, we'd still need to likely fork the changes and handle it ourselves.

@bennothommo after rereading it, this isn't as big a deal as I thought. People shouldn't be using echo or other print statements in methods called by Twig templates, they should just be returning the values they want displayed. I can't think of any decent quality plugin that would be doing that in the wild.

@LukeTowers I do not agree with you. The problem is not in the functions of echo or print, there is no point in calling them at all with twig. The problem is that now there is no way to conveniently debug your code using dd(), I think this is actually an important issue for developers using October CMS.

@Nai4rus The PR to the Twig library above will enable dd(), die(), etc. when Twig is in debug mode, which will be enabled when October itself is in debug mode. You will still be able to debug in the proper environment, it will simply stop it working on production.

On Hold until a new version of Twig is released which contains the fixes provided by this PR: https://github.com/twigphp/Twig/pull/3059

Twig 2.11.3 is now released, which should include the PR above.

@Nai4rus @jexme Would you be willing to test that version of Twig and see if it resolves the issue? Make sure that your October CMS install has debug set to true whilst doing this.

Twig 2.11.3 is now released, which should include the PR above.

@Nai4rus @jexme Would you be willing to test that version of Twig and see if it resolves the issue? Make sure that your October CMS install has debug set to true whilst doing this.

@bennothommo Yes, with version 2.11.3, with debug = true it works as it should, with debug = false it does not work, as discussed

Thank you all for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mittultechnobrave picture mittultechnobrave  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments

lukaszbanas-extremecoding picture lukaszbanas-extremecoding  路  3Comments

gergo85 picture gergo85  路  3Comments

oppin picture oppin  路  3Comments