flashSession output without html or css

Hello,

I would like to output flashSession message without any html/css format

I tried this without success:

$di->set('flash', function() {
    $flash = new \Phalcon\Flash\Session();
    $flash->setAutomaticHtml(false);
    return $flash;
});

How can I remove html/css

Thanks

Nuno



6.7k

This seems to work for me.Can you show us the code where you are outputting the message? Are you calling the output method of your flash service as it is session based? e.g

$this->flash->output();

Also make sure you haven't set-up another service called "flash" that uses Flash\Direct() as if that is defined after your session one then it will be used and that might not have automaticHtml set to false



2.5k

Hi,

Yes, it's a session based. I was using:

{{ flashSession.output() }}

shoud be:

{{ flash.output() }}

inside volt template. That's the only way it works.

But how can I check if I have a message? I'm using jquery popup window to display the success of an action and would like to validate flash.output() before inseting the javascript code in the volt template. is there a way to do it? something like this.

  {% if message is defined %}
  <script>
    $( document ).ready(function() {
      $.gritter.add({
        title: 'Info!',
        text: '{{ flash.output() }}'
    });
    return false;
  });
  </script>
  {% endif %}

Thanks



10

I haven't seen an "official" way to do this, but the following should work.

{% set output = flash.output() %}
{% if output|length %}
    <script>
        $( document ).ready(function() {
        $.gritter.add({
        title: 'Info!',
        text: '{{ output }}'
    });
    return false;
    });
    </script>
{% endif %}
edited Jun '15

I know this is old but it come top in search results so I'm going to take you through what I do. I have a situation where I need just plain old string output to integrate into a javacript call to Materialize.toast (which is a bit like a bootstrap style framework and has a little toast pop up feature, see http://materializecss.com/dialogs.html).

The html in the flash messages was making life difficult so I made my own Messages component:

within my components directory I created Messages.php

    <?php

    use Phalcon\Mvc\User\Plugin;

    /**
     * String only messages as an alternative to html formatted flash messages.
     */
    class Messages extends Plugin {

        public function set($message = null){
            if(empty($message)) return false;

            $this->session->set("message", $message);
        }

        public function has() {
            return $this->session->has("message");
        }

        public function output() {
            if($this->has()){
                $return = $this->session->get("message");
                $this->session->remove("message");
                return $return;
            }

            return false;
        }
    }

Extending Plugin allows me to use $this->session to handle the storage of the message.

I register this class (look up Phalcon Loader) and then use it in my services.php file:

    $di->set('messages', function() {
        $messages = new Messages();
        return $messages;
    }, true);

So I have set, has and output method I can use in my views. For example, say a log in fails we can set the message with $this->messages->set("Sorry, try again"); and in our view (in volt):

        {% if  messages.has() %}
            <script type="text/javascript">
               Materialize.toast("{{ messages.output() }}", 4000);
            </script>
        {% endif %}

Hey presto, I have my toast popup! You can also simply just use {{ messages.output() }} within your view.

edited Jun '15

See and Custom that... https://github.com/kimthangatm/zcms/blob/master/app/config/services.php

https://github.com/kimthangatm/zcms/blob/master/app/templates/backend/default/flashSession.volt

Replace: <strong>{{ _('gbflashsession' ~ key) }}:</strong> {{ (childItem) }} To <strong>{{ key }}:</strong> {{ childItem }}