Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Questions and Supports (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=18)
-   -   smarty questions (http://www.mysidiaadoptables.com/forum/showthread.php?t=4543)

Kyttias 06-17-2014 02:47 PM

Smarty and Conditional Templating~?
 
So I've been reading through Smarty documentation, since Mysidia is built using that templating system.

The documentation suggests we can do things such as the following in our template (.tpl) files:

PHP Code:

{if $logged_in}
Welcome, {$username}!
{else}
Please log in or sign up~
{/if} 

That's pretty neat, and conditional templating sounds fun, considering there are large portions of my menu I'd rather not display to users not signed in. Currently a $logged_in variable by that name does not exist, so only the else statement will ever appear, and I was hoping {$mysidia->user->isloggedin} and {$mysidia->user->username} would work but those are not accessible outside their scope, and, honestly, why isn't Registry::get("mysidia"); on a global scope? They're accessible in the sidebar.

My complaint with the sidebar usage is that... well... I want more freedom than that? I want to place it in anywhere in my template (.tpl) and not have to mess with deep Mysidia files every time I want to place my user's name or currency amount. If I want to display any variable on this site, I want to make full use of the Smarty templating. I think some considerations for the next version of Mysidia should be made in regards to variables accessible to us from any page, any where, any time.

In short -
Where can I/should I/do I position Registry::get("mysidia"); to make its usage global?
How do I go about shortening {$mysidia->user->username} to {$username}, {$mysidia->user->isloggedin} to {$logged_in}, and {$mysidia->user->money} {$mysidia->settings->cost} to {$currency}?

:meow:~ <3

IntoRain 06-17-2014 04:10 PM

I'm currently enjoying the joys of Smarty, after having to use it for a class this semester. I will see if I can explain this well! :D

Basically Smarty is a way to code a template without putting PHP and HTML together. You basically need to assign a PHP variable into a Smarty variable, to be able to use it in a .tpl

I usually set my variables in class_template.php's assignTemplateVars() function with

PHP Code:

$this->assign("nameToUseInTemplate",$nameOfVariable); 

and in class_frame.php with

PHP Code:

$mysidia->template->assign("temp",$var); 

So, in those files, as long as you have this:

PHP Code:

$mysidia Registry::get("mysidia"); 

You can use anything from mysidia.

--------------

To assign the loggedin variable to a Smarty variable called logged_in in class_template.php:

PHP Code:

$mysidia Registry::get("mysidia");
$this->assign("logged_in",$mysidia->user->isloggedin); 

You can now use {$logged_in} in your template.tpl file.

More examples (in class_template.php):

PHP Code:

$mysidia Registry::get("mysidia");
$this->assign("logged_in",$mysidia->user->isloggedin);

//get user's cash
$this->assign("user_cash",$mysidia->user->getcash());

//get user's username
$this->assign("username",$mysidia->user->username);

//get user's group
$this->assign("group",$mysidia->user->getusergroup());

//get user's new unread messages (3 max)
$messages $mysidia->db->select("messages", array(), "touser='{$mysidia->user->username}' and status='unread' ORDER BY id DESC LIMIT 3")->fetchAll();
$this->assign("messages",$messages);

//using messages in template.tpl (example):
{foreach from=$messages item=message}
<
a href="/messages/read/{$message.id}">
      
From:{$message.fromuser}
      
Title:{$message.messagetitle}
      
Date:{$message.datesent}
</
a>
{/foreach} 


Kyttias 06-17-2014 04:54 PM

I've still yet to get it to work completely.

Let's start with what I've successfully done -
In class_template.php's assignTemplateVars() function I've added:
Code:

$mysidia = Registry::get("mysidia");
$this->assign("logged_in",$mysidia->user->isloggedin);
$this->assign("username",$mysidia->user->username);
$this->assign("cash",$mysidia->user->getcash());
$this->assign("currency",$mysidia->settings->cost);

And in template.tpl:
Code:

{if $logged_in}
Welcome, <a href="{$home}profile/view/{$username}"><b>{$username}</b></a>!
<br/>
You have {$cash} <b>{$currency}</b>.
{else}
Please sign up or log in~!
{/if}

And this displays the correct outcome! Yay!

However, using -
Code:

$this->assign("group",$mysidia->user->getusergroup());
Results in a blank white page. :desudesudesu:

I'm as of yet unsure how or if the messages work, but I'd like it to just display a number of unread messages a user has, and possibly 0 if there is none? I have no messages, so nothing appears. :happycbig: It looked like it would display more than just a number, but information about the messages. I don't think I need that, but a number next to an envelope icon is floating in my head as a pleasant sight.

Also, thank you very much!

IntoRain 06-17-2014 05:21 PM

Sorry! It's $mysidia->user->getgroup()! Which will return one of the groups available in your database (rootadmins, admins,registered,artists,banned,visitors).

Messages is an array containing multiple message objects, that's why I use foreach there, it goes through every "item" in the messages variable. Each Message contains a title, content, date, etc... To get just the number of new messages:

$messages = $mysidia->db->select("messages", array(), "touser='{$mysidia->user->username}' and status='unread'")->rowCount();
$this->assign("messages",$messages);

Kyttias 06-17-2014 05:29 PM

Awesome! Very soon I'll have replaced the entire sidebar. Added this, too:
Code:

$online = $mysidia->db->select("online", array(), "username != 'Visitor'")->rowCount();
$offline = $mysidia->db->select("online", array(), "username = 'Visitor'")->rowCount();
$this->assign("population","{$online} Users Online, {$offline} Guests");

I've always wanted this in the footer, now I finally can.
Code:

<a href="{$home}/online">{$population}</a>
And as a follow up to something we had done in another thread, and in case anyone is paying attention to this one, this will allow one to display the current server time with {$time} and it will show up as, for example, 07:37 PM:
Code:

$now = new DateTime();               
$display = $now->format('h:i A');               
$this->assign("time",$display);


Also~!!

I almost forgot Mysidia had a friend's list system. The 'send friend request' button is currently on the last tab of the profile... I'll be moving that to the front where it's easier to find. However, Is there a table in the database that may be able to help me make a notification system for unaccepted/new friend requests? And I seem to be overlooking where I can link users to a list of their own friends, other than their profile...

IntoRain 06-17-2014 06:55 PM

Hehe it's good you are getting used to it ^^ The table friend_requests might help, status = pending means unaccepted.

The table users keeps a list of friends as a string (each friend separated by a comma). The way profile does it is to create a Friendlist object (class_friendlist), with the user object. The Friendlist object then creates an array of integers (the friends IDs) by dividing the entire string (explode(",",string)). I'd look into class_userprofile.php (getfriends() function) and class_friendlist.php depending on what you want to do. mysidia->user->getfriends() seems to return the full string with friends as well, if you want to use Explode() on it yourself. After Explode() you get an array with all the ids, you can then create a Member object using those ids.

Kyttias 06-17-2014 08:30 PM

Ah, what if I just wanted the number of pending friend requests, but not a list of names? Is that simpler? (Is exploding supposed to sound dangerous, because it does.) I'll take a closer look in a few minutes, but I really can't thank you enough for all this help. ovo~

I could probably make a tooltip with a fun combination of Smarty conditionals and Javascript, that if the 'pending requests' are 'greater than 0', hovering over the notification would pull down a notification with, if I knew how to get this information, the pending friend(s) names and links to confirm or deny them without the need to travel to a new page. But that sounds more ambitious than I want to trouble myself with for tonight.

On a similar vein, if the messages in the inbox were 'greater than zero', to hover over the number and show some basic information, like the message titles (wherein clicking a specific title would take you to that specific message as well) and the senders, of about the first three messages, sort of like you had originally given.

I'll probably accumulate the things I've learned here into a nice convenient post for others eventually. My intention is, of course, to give people a way to easily install something like this, to go under a header image of their choosing:
http://fc05.deviantart.net/fs70/f/20...as-d7mttaj.png

Kesstryl 06-17-2014 09:04 PM

You wouldn't mind making a mod for this and putting it in the mod section, would you please? I like your changes and I'd love to have something similar for my site. I don't want to copy you, but something simple like showing how much cash you have on each page, unread messages, etc, those are must haves.

Kyttias 06-17-2014 09:11 PM

Quote:

Originally Posted by Kesstryl (Post 30204)
You wouldn't mind making a mod for this and putting it in the mod section, would you please? I like your changes and I'd love to have something similar for my site. I don't want to copy you, but something simple like showing how much cash you have on each page, unread messages, etc, those are must haves.

^^ I think I will, given enough time. For now, you're more than welcome to reference any information here! It's relatively straight forward, luckily. I think I also might release a theme around it (one that expands on Bootstrap). Thus far. . .

Hall of Famer 06-18-2014 04:33 AM

It should be very easy to make a friend request notification system. The mechanism can be very similar to new PM notification, my suggestion is to look at how PM notification is generated and make something similar to this. In fact, this feature is planned for Mys v1.4.0.


All times are GMT -5. The time now is 07:05 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.