drogon icon indicating copy to clipboard operation
drogon copied to clipboard

How to correctly handle data here with Drogon

Open ericel opened this issue 2 years ago • 0 comments

I have an object that is passed through my controller as

   User currentUser = User::ensureUserInSession(req);
    data.insert("user", currentUser);

Now I want to use this in my .csp template file as for instance check if user is authenticated

<body class="<%= user.is_authenticated ? "authenticated" : "anonymous" %>"> For some reason the above line doesn't even compile.

Then even doing something like this in the .csp file [[ user.is_authenticated ]] displays nothing.

Which have pushed me to do this:

 User currentUser = User::ensureUserInSession(req);
   // Insert the user data into ViewData
   data.insert("username", currentUser.username);
   data.insert("uid", currentUser.uid);
   data.insert("is_authenticated", currentUser.is_authenticated ? "true" : "false");
   std::string bodyClass = currentUser.is_authenticated ? "" : "anonymous";
   data.insert("body_class", bodyClass);

and in my template file:

<body class="[[ body_class ]]">

This works but I don't see this as sustainable. Obviously, I am new here and there must be a better way of doing this that I am not aware of yet. Someone point me to the right place.

ericel avatar Nov 02 '23 12:11 ericel