Flash variables\hive values null in template

74 views
Skip to first unread message

Yoeri Nijs

unread,
Aug 14, 2019, 4:05:16 PM8/14/19
to f3-fra...@googlegroups.com
For a new project, I work with Ikkez's Flash plugin. This plugin is great for displaying messages and I am using it with a lot of joy for another project as well. 

Now, for a new project, I am working with Flash again, but the plugin does not work. The messages are null after a addMessage() and a direct get request to '/', while this does work for the other project. After some debugging, I am unable to determine what the cause is. Maybe you guys have an idea? I assume it has something to do with my autoload in globals.ini, but I am not sure (all other views do work, though). 

The setup is as follows:

- app
 
- config
   
- globals.ini
   
- message.ini
   
- ...
 
- controllers
   
- ...
 
- models
   
- ...
 
- core
   
- component
     
- message
       
- Message (is just a wrapper for Flash)
       
- ...
     
- ...
 
- views
   
- inc
     
- components
       
- message.htm
       
- ...
     
- ...
    layout
.htm


Globals.ini
[globals]

; Load directly
AUTOLOAD = ../app/
UI = ../app/views/

Message.ini (contains just some styling and some default labels)
[message]

MESSAGE_CSS_CONTAINER_ERROR = bg-teal-100 border-t-4 rounded-b text-teal-900 px-4 py-3 shadow-md
MESSAGE_CSS_CONTAINER_DANGER = bg-teal-100 border-t-4 rounded-b text-teal-900 px-4 py-3 shadow-md
MESSAGE_CSS_CONTAINER_INFO = bg-teal-100 border-t-4 rounded-b text-teal-900 px-4 py-3 shadow-md
MESSAGE_CSS_CONTAINER_WARNING = bg-teal-100 border-t-4 rounded-b text-teal-900 px-4 py-3 shadow-md
MESSAGE_CSS_CONTAINER_SYSTEM = bg-teal-100 border-t-4 rounded-b text-teal-900 px-4 py-3 shadow-md
MESSAGE_CSS_TITLE = font-bold
MESSAGE_CSS_MSG = text-sm

MESSAGE_NO_RIGHTS = Geen rechten
MESSAGE_INVALID_ID = Ongeldig id

MESSAGE_UPDATED_GENERAL = Succesvol bijgewerkt
MESSAGE_CREATED_GENERAL = Succesvol aangemaakt
MESSAGE_DELETED_GENERAL = Succesvol verwijderd

Message.php
<?php

namespace core\component\message;

use Base;
use Flash;
use Prefab;

class Message extends Prefab {

    private static $errorCssClass = 'error';

    private static $successCssClass = 'success';

    private static $infoCssClass = 'info';

    private static $warningCssClass = 'warning';

    private static $systemCssClass = 'system';

    // Errors
    public static function errorNoRights() {
        $msg = Message::construct(Base::instance()->get('message.MESSAGE_NO_RIGHTS'));
        Flash::instance()->addMessage($msg, Message::$errorCssClass);
    }

    public static function errorInvalidId() {
        $msg = Message::construct(Base::instance()->get('message.MESSAGE_INVALID_ID'));
        Flash::instance()->addMessage($msg, Message::$errorCssClass);
    }

    public static function errorCustom($msg) {
        Flash::instance()->addMessage(Message::construct($msg), Message::$errorCssClass);
    }

    public static function displayErrors($errors) {
        $msg = '';
        foreach($errors as $error) {
            foreach(array_values($error) as $value) {
                $msg .= '- ' . $value . '.<br />';
            }
        }
        Flash::instance()->addMessage($msg, Message::$errorCssClass);
    }

    // Success
    public static function successUpdated() {
        $msg = Message::construct(Base::instance()->get('message.MESSAGE_UPDATED_GENERAL'));
        Flash::instance()->addMessage($msg, Message::$successCssClass);
    }

    public static function successCreated() {
        $msg = Message::construct(Base::instance()->get('message.MESSAGE_CREATED_GENERAL'));
        Flash::instance()->addMessage($msg, Message::$successCssClass);
    }

    public static function successDeleted() {
        $msg = Message::construct(Base::instance()->get('message.MESSAGE_DELETED_GENERAL'));
        Flash::instance()->addMessage($msg, Message::$successCssClass);
    }

    public static function successCustom($msg) {
        Flash::instance()->addMessage(Message::construct($msg), Message::$successCssClass);
    }

    // Info
    public static function infoCustom($msg) {
        Flash::instance()->addMessage(Message::construct($msg), Message::$infoCssClass);
    }

    // Warning
    public static function warningCustom($msg) {
        Flash::instance()->addMessage(Message::construct($msg), Message::$warningCssClass);
    }

    // New
    public static function systemCustom($msg) {
        Flash::instance()->addMessage(Message::construct($msg), Message::$systemCssClass);
    }

    private static function construct($msg) {
        $substr = substr($msg, strlen($msg) - 1, strlen($msg));
        switch ($substr) {
            case '.':
            case '!':
            case '?';
                break;
            default:
                $msg .= '.';
        }
        return $msg;
    }
}

The message.htm component (uses Tailwind). I should use a switch here ;)
<repeat group="{{ \Flash::instance()->getMessages() }}" value="{{ @msg }}">
    <check if="{{ @msg.status === 'error'}}">
        <true>
            <div class="{{ @message.MESSAGE_CSS_CONTAINER_ERROR }}" role="alert">
        </true>
    </check>
    <check if="{{ @msg.status === 'success'}}">
        <true>
            <div class="{{ @message.MESSAGE_CSS_CONTAINER_SUCCESS }}" role="alert">
        </true>
    </check>
    <check if="{{ @msg.status === 'info'}}">
        <true>
            <div class="{{ @message.MESSAGE_CSS_CONTAINER_INFO }}" role="alert">
        </true>
    </check>
    <check if="{{ @msg.status === 'warning'}}">
        <true>
            <div class="{{ @message.MESSAGE_CSS_CONTAINER_WARNING }}" role="alert">
        </true>
    </check>
    <check if="{{ @msg.status === 'system'}}">
        <true>
            <div class="{{ @message.MESSAGE_CSS_CONTAINER_DEFAULT }}" role="alert">
        </true>
    </check>

        <div class="flex">
            <div class="py-1"><svg class="fill-current h-6 w-6 text-teal-500 mr-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M2.93 17.07A10 10 0 1 1 17.07 2.93 10 10 0 0 1 2.93 17.07zm12.73-1.41A8 8 0 1 0 4.34 4.34a8 8 0 0 0 11.32 11.32zM9 11V9h2v6H9v-4zm0-6h2v2H9V5z"/></svg></div>
            <div>
                <check if="{{ @msg.status === 'error'}}">
                    <true>
                        <p class="{{ @message.MESSAGE_CSS_TITLE }}">Fout</p>
                    </true>
                </check>
                <check if="{{ @msg.status === 'success'}}">
                    <true>
                        <p class="{{ @message.MESSAGE_CSS_TITLE }}">Geslaagd</p>
                    </true>
                </check>
                <check if="{{ @msg.status === 'info'}}">
                    <true>
                        <p class="{{ @message.MESSAGE_CSS_TITLE }}">Informatie</p>
                    </true>
                </check>
                <check if="{{ @msg.status === 'warning'}}">
                    <true>
                        <p class="{{ @message.MESSAGE_CSS_TITLE }}">Waarschuwing</p>
                    </true>
                </check>
                <check if="{{ @msg.status === 'system'}}">
                    <true>
                        <p class="{{ @message.MESSAGE_CSS_TITLE }}">Systeembericht</p>
                    </true>
                </check>
                <p class="{{ @message.MESSAGE_CSS_MSG }}">{{ @msg.text | raw }}</p>
            </div>
        </div>

    </div>
</repeat>


In layout.htm, I display the components view like so. 
<!-- Messages -->
<include href="inc/components/message.htm" />

When I change to content of message.htm to 'foo' and nothing else, 'foo' is displayed. This tells me that Flash might not be reachable by the template, hence my autoload assumption.

ikkez

unread,
Aug 15, 2019, 5:50:53 AM8/15/19
to f3-fra...@googlegroups.com
Are you sure to use the template engine to render the template? (not view or preview?!)
The SESSION is also working correctly?
If Flash is not be available, I guess it would raise an error 500 instead of just doing nothing.

Yoeri Nijs

unread,
Aug 15, 2019, 3:31:18 PM8/15/19
to f3-fra...@googlegroups.com
I am using Template for GET requests. This works:

// GET REQUEST
function getFunction() {
 
Message::infoCustom('foo');
  $this
->render()
}


protected function render(): void {
     $template
= new Template();
     echo $template
->render('layout.htm');
 
}


However, this does not work (i.e.: I do not see any message). I am just initializing this with the router:

// POST REQUEST
function postFunction() {
 
Message::infoCustom('foo');
  $this
->f3->reroute('/');
}

// ROUTE '/'
function getRequest() {
  $this->render();
}



Is my implementation wrong?


Op donderdag 15 augustus 2019 11:50:53 UTC+2 schreef ikkez:

Yoeri Nijs

unread,
Aug 15, 2019, 3:52:21 PM8/15/19
to Fat-Free Framework
Well, it turns out that setting a SQL session prior to displaying any messages is responsible for this behaviour.

So executing new Session() before all of this breaks everything. I do not understand it atm, so I will debug it later.

Op donderdag 15 augustus 2019 21:31:18 UTC+2 schreef Yoeri Nijs:
Reply all
Reply to author
Forward
0 new messages