What is new in Mys v1.3.6: Custom Smarty Templates

Forum
Last Post
Threads / Messages
We are near the end of the July, and the first beta release of Mys v1.3.6 is coming closer. About 2 weeks ago I managed to get PHP 7/8 compatibility working, and now I am ready to introduce another new QoL update for the new version. This one is referred to as Custom Smarty Templates, which allows users to define custom smarty templates for HTML elements. It may not be useful for absolute beginners, but will make lives significantly easier for the advanced users who are customizing the built-in view/pages from original Mysidia Adoptables script, or even building their own views/pages.

I will use an example to demonstrate what this means since the terms may sound enough confusing. For the user account index page, the html is composed programmatically with Mysidia's GUI API:

PHP:
        $settings = new Comment("Account Settings");
        $settings->setBold();
        $settings->setUnderlined();
        $document->add(new Comment);
        $document->add($settings);
        $document->add(new Link("myadopts", "Manage Adoptables", TRUE));
        $document->add(new Link("profile/view/{$mysidia->user->getID()}", "View Profile", TRUE));
        $document->add(new Link("account/password", "Change Password", TRUE));
        $document->add(new Link("account/email", "Change Email Address", TRUE));
        $document->add(new Link("account/friends", "View and Manage FriendList", TRUE));
        $document->add(new Link("account/profile", "More Profile Settings", TRUE));
        $document->add(new Link("account/contacts", "Change Other Settings"));

If you are already familiar with Mysidia's code, you'd notice that what this above code does is basically to compose the following HTML:

8fEj0te.png


Since I am a programmer myself, the PHP code makes enough sense for me. For non-coders, they will struggle to understand what it means. The worse part is, they do not know how to create HTML elements programmatically if they want to customize the generated HTML, and will end up wrapping a long HTML string inside a text variable. With Mys v1.3.6, theres no need for such ugly workaround anymore. It is now possible to create smarty template file, which is effectively just HTML files that allow PHP variables to be safely and elegantly embeded into the file without using PHP tags.

To accomplish this, simply create a file accountlinks.tpl in folder /templates/common with the following HTML code(note $scriptPath and $mysidia are variables automatically assigned to a smarty template file so you can use the dynamic values):

HTML:
<b>Account Settings</b>
<a href='{$scriptPath}/myadopts'>Manage Adoptables</a>
<br>
<a href='{$scriptPath}/profile/view/{$mysidia->user->getID()}'>View Profile</a>
<br>
<a href='{$scriptPath}/account/password'>Change Password</a>
<br>
<a href='{$scriptPath}/account/email'>Change Email Address</a>
<br>
<a href='{$scriptPath}/mys136b/account/friends'>View and Manage FriendList</a>
<br>
<a href='{$scriptPath}/mys136b/account/profile'>More Profile Settings</a>
<br>
<a href='{$scriptPath}/mys136b/account/contacts'>Change Other Settings</a>

And on the account view file, replace the HTML GUI code by this line which will load a template file(assuming the file accountlinks.tpl is located in /templates/common directory of your script path):

PHP:
$document->addLangvar($this->addTemplate("accountlinks"));

Now it will work exactly the same as the HTML GUI version, but it should look a lot easier to understand and use for non-coders. Note you may add multiple custom smarty template files for the same view/page, you can also use the HTML GUI API and Custom Smarty Templates together with each other as I've designed them to interoperate with each other. If you need to pass variables to a template file, simply use the method setField($field, $value) at the controller action(ie. index() method in account controller for this example). If you are unsure how to accomplish this, make a thread on the forum and I will explain it in a follow-up post to clear up some confusions.

Hope you all enjoy the new features introduced so far in Mys v1.3.6, I will make another 1-2 more blog post about the other QoL updates before the beta release.
  • Like
Reactions: Trua

Comments

So will the old method of using PHP methods to generate the html be gone in this update? It'll all be based on smarty templates?

This is awesome and will definitely provide a lot more flexibility and perhaps performance if we utilize smarty's caching system!
 
So will the old method of using PHP methods to generate the html be gone in this update? It'll all be based on smarty templates?

This is awesome and will definitely provide a lot more flexibility and perhaps performance if we utilize smarty's caching system!
Yes both options will be available. The PHP GUI approach is better if you need to pass nontrivial amount of data to the view, ie. a table showing list of adoptables, or an edit form of item. The Smarty template approach is more convenient if your HTML is static or contains very few dynamic data.
 
I'm having trouble getting this to work on my index page. Here is my index code, and I will attach what I put into my templates/common folder

Code:
<?php

namespace View\Main;
use Resource\Core\Registry;
use Resource\Core\View;

class IndexView extends View{
    
    public function index(){
        $mysidia = Registry::get("mysidia");
        if($mysidia->systems->site != "enabled"){
            $document = $this->document;       
            $document->setTitle("An error has occurred...");
            $document->addLangvar("The admin has turned off the site for maintenance, please come back and visit later.");       
        }
        elseif($mysidia->user->isLoggedIn()){
                /* $frame = $mysidia->getFrame();
                $frame->getDocument("home"); */
                $document = $this->document;
                $document->setTitle("Home Page");
                $document->addLangvar($this->addTemplate("homeview"));
        }
        else{
            $frame = $mysidia->getFrame();
            $frame->getDocument("index");
        }
    }
}

and in templates/common/homeview.tpl

Code:
<b>Things to do</b>
<a href='{$scriptPath}/myadopts'>Visit your pets</a>
<br>
<a href='{$scriptPath}/daycare'>Help at the Daycare</a>
<br>
<a href='{$scriptPath}/profile'>Visit Members</a>
<br>
<a href='{$scriptPath}/pound'>Visit the Adoption Center</a>
<br>
<a href='{$scriptPath}/market'>Go to the Market</a>
<br>
 
I forgot to add the error code

#​
Time​
Memory​
Function​
Location​
Call Stack​
( ! ) Error: Call to a member function isEmpty() on null in C:\wamp64\www\mysidia1.3.6\resource\core\view.php on line 318
Fatal error: Uncaught Error: Call to a member function isEmpty() on null in C:\wamp64\www\mysidia1.3.6\resource\core\view.php on line 318
1​
0.0004​
364224​
{main}( )...\index.php:0
2​
0.0010​
366880​
IndexController::main( )...\index.php:88
3​
0.0403​
1126632​
IndexController->run( )...\index.php:84
4​
0.0526​
1139976​
Resource\Core\FrontController->render( )...\index.php:78
5​
0.0526​
1139976​
View\Main\IndexView->index( )...\frontcontroller.php:120
6​
0.0527​
1139976​
Resource\Core\View->addTemplate( $template = 'homeview', $folder = ??? )...\indexview.php:21
 
Since I can't edit or delete my previous comments, I fixed this a long time ago and I don't remember what I did. This code does work perfectly:

elseif($mysidia->user->isLoggedIn()){
$document = $this->document;
$document->setTitle("Home Page");
$document->addLangvar($this->addTemplate("homeview"));
}
 
  • Forum Contains New Posts
  • Forum Contains No New Posts

Blog entry information

Author
Hall of Famer
Views
913
Comments
6
Last update

More entries in Mysidia

More entries from Hall of Famer

Forum statistics

Threads
4,260
Messages
33,016
Members
1,601
Latest member
Fortuna
BETA

Latest Threads

Top