We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Flash Session weirdness

Hi there,

I've created the following partial:


{% if flashSession.getMessages()|length > 0 %}

  <div class="row">
    <div class="small-12 columns">

      {{ flashSession.output() }}

    </div>
  </div>

{% endif %}

If I leave the length test in, when it is true, my markup is output but flashSession.output() returns nothing. If I comment out the length test, when there are messages, flashSession.output() works as expected.

It seems like calling getMessages() to check the count removes the messages? Basically, I'm checking for existence of messages to avoid outputting extraneous markup when there is no need to.

Am I misunderstand something?

Many thanks.



8.1k

I guess that might just as well be the case.. I never worked with it this way, and the following is not documented in the API reference, but does this work: .getMessages(null, false) ?

Ref: https://phalcon.agent-j.ru/en/1.3.0/Phalcon/Flash/Session#getMessages-details

I guess the length check isn't necessary (an empty array gives false).

I also notice the has method.. Perhaps that's intended for what you're trying to accomplish?



38.8k

HI @renski - thanks for the link! I have never come across that API reference before. There is much more information in there than I have been seeing in the Docs section :)

I'll check this out further based on your suggestions and will report back with my progress. I appreciate your help.



15.1k

If i remember correctly, all partials are sanboxed, so you have to explicitly pass flashSession as a variable into your partial.

{{ partial("partials/footer", ['flashSession': flashSession ]) }}