Skip to content

SebKay/noticeable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Notice

Easily output a simple flash message/notice to the page, but only once.

How to install

It's recommended you install this package via Composer.

composer require sebkay/php-notice

The notice is session based and will be removed on the next page refresh.

First you'll need to start a session. Put this at the very start of your project files (so it's the first thing that loads):

session_start();

You'll then need to include the Composer autoloader so you have access to the package.

require get_template_directory() . '/vendor/autoload.php';

How to use

Setting the notice

You can set a notice using the ::set static method and passing the message (as well as the type).

The type can be anything you want, but error and success are good standards to follow.

PHPFN\Notice::set([
    'message' => 'Please enter an email address.',
    'type'    => 'error'
]);

Getting the notice

When you get a notice it will return an array, like so:

$notice = PHPFN/Notice::get();

var_dump($notice);

// Will return
array(2) {
  ["message"]=>
  string(12) "Please enter an email address."
  ["type"]=>
  string(5) "error"
}

Using the notice

Once you have the array you can do whatever you want with it, like load a PHP file with some HTML to format the message.

Twig

I'm a big fan of Twig, so I would do something like this:

echo $twig->render('notice.twig', PHPFN/Notice::get());

Then I'll have the corresponding notice.twig file laid out like so:

{% if message %}
    {% if type == 'success' %}
        {% set css_class = 'notice--success' %}
    {% elseif type == 'error' %}
        {% set css_class = 'notice--error' %}
    {% endif %}

    <div class="notice {{ css_class }}">
        <p class="notice__title">
            {{ message | raw }}
        </p>
    </div>
{% endif %}

About

A simple way to show a session based flash message.

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Contributors

Languages