Marching ahead.. Week 2,3 Summary

Nilesh Prasad
GSoC’17 Diary
Published in
3 min readJun 16, 2017

We are into third week of GSoC and all is going well till now! I am learning new concepts daily. One of the major milestones of my GSoC project is to remove Smarty Template Engine from the forms that are using it, as its use is deprecated in existing LibreEHR application .

What is Smarty Template Engine?

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

But, all the things which you could achieve using a template engine can already be achieved using plain PHP as PHP itself can act as a template engine . This is a great article to elaborate on this.

For the last two weeks, I was busy thinking about the proper approach to remove Smarty Template Engine from existing LibreEHR forms . I could have either written the existing Controller classes from scratch or simply modify the existing Controller classes to work independent of the Smarty class.

I decided to go with the latter one. This required me to understand some of the Smarty defined functions like fetch() and display(). Although, these two functions appear to behave the same, they are quite different. The fetch() function is used to return the template output but it doesn’t display the template unlike display() which displays the template. Both these functions need to have valid template resource type and path as a parameter. One can also set cache_id and compile_id as optional parameters. Replacing display() functions was quite easy and I just needed to replace it with require_once() with template path as the parameter. However, fetch() functions motivated me to learn about output buffering in PHP.

In simple words, output buffering allows you to have output of PHP (primarily generated by echo) stored into a memory (ie. buffer) instead of immediately transmitting to the browser or terminal.

Preventing output:

ob_start(); // turns on output buffering
$foo->bar(); // all output goes only to buffer
ob_end_clean(); // clears buffer and closes buffering

Capturing the output to a variable:

ob_start(); // turns on output buffering
$foo->render(); // output goes only to buffer
$output = ob_get_contents(); // stores buffer contents to the variable
ob_end_clean(); // clears buffer and closes buffering

Pair ob_get_contents() and ob_end_clean() can be replaced by a single function ob_get_clean().

$output = ob_get_clean(); // stores buffer content into variable and turns off buffering

This was all I was looking for to replace the use of fetch().Easy!

In these two weeks , I learnt a lot about Object-Oriented PHP and MVC. So, currently I am done replacing the use of Smarty from Administration/Practice section of the application. There are many more forms using Smarty and they are to be removed soon. I also developed a basic MVC to port all the existing forms to the new MVC , if I get some time.

This concludes this update. Thanks!

--

--

Nilesh Prasad
GSoC’17 Diary

Backend Engineering | Scalability | Distributed Systems