Home Community Mys-Script Creative Off-Topic |
|
|
Thread Tools | Display Modes |
#1
|
||||
|
||||
Smarty Tips: Things you can do with Templates
Mysidia is built with the Smarty template system. Here's some fun facts:
You can write multiple conditions using the {if} {elseif} {elseif} {else} {/if} style. In Smarty, you can also implement a nested {if} {/if} block. You can display the current date in your template with {$smarty.now|date_format} and it will print in the format Jul 15, 2014. For the time, check out {$smarty.now|date_format:"%H:%M"} for military time that prints 16:45, or {$smarty.now|date_format:"%I:%M %p"} for 04:45 PM. If you know how php prints dates, you can probably figure out how to customize this a ton! For fun, pop open classes/class_template.php and inside private function assignTemplateVars(){ after it's row of this->s are done, add these: PHP Code:
In your template.tpl, you can now do this: Code:
{if $logged_in} <a href="{$home}profile/view/{$mysidia->user->username}">{$username}</a> ${$cash} {$mysidia->settings->cost} <a href="{$home}messages">{$messages} Messages</a> <a href="{$home}login/logout">Log Out</a> {else} Please <a href="{$home}login/logout">Log In</a> or <a href="{$home}register">Sign Up</a>! {/if} - only display if a user is logged in - print their username, which will link to their profile - show how much currency they have and the name of your currency - show how many messages they have, and a link to go check them out - show a log out link when they are logged in - when they aren't logged in, it will ask them to log in or sign up You'll definitely want to work around this and make it pretty. ^_~ Another example might be a message that shows up 30% of the time: Code:
{if rand(0,100) <= 30} Winner! {/if}
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it. Last edited by Kyttias; 04-03-2015 at 11:01 AM. |
#2
|
||||
|
||||
+Bump plus more examples of possibilities:
(If using the $cash variable set in the above post!) Code:
{if $cash < 7777}User has more than 7777 currency!{/if} {if $cash > 7777}User has less than 7777 currency!{/if} PHP Code:
Code:
<a href="{$home}online">{$online}</a> Code:
{if $online > 50}More than 50 users online!{/if} {if $online < 10}Less than 10 users online!{/if} PHP Code:
And back into classes/class_template.php, you can set up messages to display at specific times of the day (server time - good for promo codes people can't cheat and use their computer's clock to get): PHP Code:
Keep it mind that you can infinitely combine if statements with Smarty in your template. So imagine something with a 20% chance of happening only if it is between noon and five and there are less than x amount of people online.
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it. |
#3
|
||||
|
||||
Amazing tips! Thank you so much! :)
|
#4
|
||||
|
||||
I'm glad someone appreciates it!
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it. |
#5
|
||||
|
||||
Ah, here's another one, this will display a user's avatar using {$avatar} in the template:
PHP Code:
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it. |
#6
|
||||
|
||||
Since I was asked to explain all this in more detail while helping someone, here's a more detailed explanation of how all this works.
Ok, so, as I said, let's open up classes/class_template.php. Find where private function assignTemplateVars(){ is. You'll see this inside of it: PHP Code:
Alright, beneath this small tower of $this->assign();s, we're going to assign some variables of our own. In this example, the user's current currency and the name of your site's currency. First, we will need the line $mysidia = Registry::get("mysidia");. We need this so we can have the variable '$mysidia' contain a dynamic, yet static, link to the parent class called "mysidia", so we can invoke methods and properties from it. Bellow you'll see that 'user' and 'settings' are child classes of '$mysidia' and the functions and variables that come after them are literally found inside classes/class_user.php and classes/class_settings.php. You only need to define $mysidia once inside of a function, so best to do it very early on. The next line we'll need is $this->assign("cash",$mysidia->user->getcash());. We're calling the template class, '$this' since its the one we're currently in, to use the function 'assign()' to assign a Smarty variable to the word "cash", and the variable we're assigning is the result of the 'getcash()' function found inside the 'user' class, a child class of the '$mysidia' class. And finally, this is optional -- if you want the name of your currency as defined in your settings (hint: inside the 'settings' class this variable is known as 'cost' and is being pulled from the database table adopts_settings) you can obtain it using the line $this->assign("currency",$mysidia->settings->cost);, which will assign the variable 'cost' inside the 'settings' class, a child class $mysidia, to the word "currency". So, here we go: PHP Code:
PHP Code:
Now that these are in place, save the template class file and be sure to reupload it to your server. You can now head over to your template file, template.tpl -- the one I had you create for your theme in the original post. We assigned data to the variables {$cash} and {$currency}. Place these wherever you like, save, upload the updated file, and go view your site! Great. Now to only make them display if the user is logged in. Smarty can handle some basic if-statements inside curly braces, like this: PHP Code:
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Tips for theme? | Abronsyth | Templates and Themes | 6 | 01-03-2013 07:04 PM |
Any tips for writing a TOS? | Kesstryl | Feedback and Suggestions | 5 | 03-30-2012 07:37 PM |
Some Site Planning Tips | Tequila | Tutorials and Tips | 2 | 04-08-2011 10:17 AM |
Templates | CrystalShards | Suggestions and Feature Requests | 1 | 07-18-2009 01:48 PM |
templates | aroymart | Questions and Supports | 15 | 02-18-2009 02:46 PM |
What's New? |
What's Hot? |
What's Popular? |