View Single Post
  #6  
Old 04-03-2015, 10:42 AM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 91,507
Kyttias is on a distinguished road
Default

In normal PHP and HTML, yes. However...

In this case, the normal layout is not HTML, so you cannot add PHP directly to it. The 'layout' in a graphical sense, is a template file made with Smarty variables. Smarty is a commonly used PHP templating engine, and the files have the .tpl extension - they're a mix of HTML and Smarty variables. Above, I converted your files over. Very simple, all I had to do was paste the HTML in and replace a few key aspects with variables. So, the answer? You don't add it to the normal layout, you add it as a Smarty variable and then use it like as a variable inside curly braces inside the template file.

In the thread I linked in my original reply, I explain how to construct a Smarty variable inside classes/class_template.php to use inside your theme's template files.

And, yes, as you can imagine, a template means it is used on every single page. Perfect for a bar of user information at the top of the page.

For the sake of not leaving this thread, I'll tone down the tutorial in the thread I linked to use just one example - displaying the total user currency as a Smarty variable inside your theme's Smarty template.

Let me first take a moment to recommend a text editor meant for code -- Notepad++ is free, but I personally recommend Sublime Text, an even more powerful text editor, which comes with a free trial. These sorts of text editors come with syntax highlighting and the ability to create 'project' folders that can be searched through. Mass searching through hundreds of files without having to open them all one at a time? Win.

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:
$this->assign("version"Mysidia::version);
$this->assign("root"$this->scriptRoot);
$this->assign("home"$this->tempRoot);
$this->assign("temp"$this->temp);
$this->assign("css"$this->css);
$this->assign("js"$this->js); 
Cool. Leave those there. These are defining variables that help you in the template file to link places easily. (Take a peek in your header.tpl, you'll see a couple of them being used -- {$home}{$temp} -- see?)

Alright, beneath this small tower of $this->assign();s, we're going to assign some variables of our own. Because you asked, the user's current 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:
$mysidia Registry::get("mysidia");
$this->assign("cash",$mysidia->user->getcash());
$this->assign("currency",$mysidia->settings->cost); 
Ok, our variables are now ready to use. But what happens if the user is not logged in? Good question. The {$cash} variable will remain empty, but the {$currency} variable still holds the name of the site's currency. If you want this information to only display while the user is logged in, you'll also want this line:

PHP Code:
$this->assign("logged_in",$mysidia->user->isloggedin); 
{$logged_in} is now also a variable, and I'll show you how to use it in a sec.

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:
{if $logged_in}
        {
$cash} {$currency}
{else}
        
You are not logged in! <a href="{$home}register">Make an account?</a>
{/if} 
You'll probably want a few other things in there, so please review the thread I originally linked explaining all this for more help.

That should be all, as far as assigning variables that can be used to display data in the template goes.

If you have questions about making custom pages that can use PHP, rather than making new pages through the rather restrictive text-only WYSIWYG page maker in the AdminCP, go ahead and make a new thread and I'll help out there? ^^
__________________
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:19 AM.
Reply With Quote