How to report messages to user

In the course of executing a program you need to report errors / success messages to the user. But you don’t want to mix your code with HTML, right? (A quick explanation on separating presentation from business / data logic.)

So here’s what I use:

  1. span class=”coMULTI”>/**
  2. * Factory function, ensures that
  3. * MessageStack is a singleton
  4. *//**
  5. * Returns stack contents
  6. */

When I want to report something I call:

  1. $messageStack = MessageStack::create();
  2. $messageStack->add(‘ok’, ‘Record saved.’);

Then in your main file you run this:

  1. span class=”st0″>"messages", $messageStack->get());

Include this into template (preferably the part that is included into every template):

  1.  
  2.  
  3. <div class="message {$type}"></div>
  4.  
  5.  

And then in css, define all message types:

  1.  

Question

Do you use something like this or something more clever? Share your wisdom :-)

Leave a Reply