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.
 
  • Forum Contains New Posts
  • Forum Contains No New Posts

Blog entry information

Author
Hall of Famer
Views
397
Comments
3
Last update

More entries in Mysidia

More entries from Hall of Famer

Forum statistics

Threads
4,191
Messages
32,492
Members
1,567
Latest member
Enigmas
BETA

Latest Threads

Top