Welcome Guest, Not a member yet? Register   Sign In
My template library, please share your thoughts.
#1

[eluser]Twisted1919[/eluser]
So, i have my own template library which i use for months, it has been improved along the time and i could say it is very mature in this moment .
The reason i am writing this post is to present the features i have so far and get advices in what to improve, then release the library for CI community if this post proves that is useful.

Sample codes
-------------------------
Create a theme and use it
Will load the welcome.php file that is under /views/website/ folder.
Code:
$this->template->set('theme_path','website')->load('welcome_message',$data);

Make the theme structure like :
--views
---welcome
-----pages (the views will stay here)
-----css (stylesheets here)
-----js (javascripts here)
-----images (images here)
Code:
$this->template
     ->set('pages_path','pages')
     ->set('css_path','css')
     ->set('js_path','js')
     ->set('images_path','images')
     ->load('welcome_message',$data);
Also in above exp, in the view file, the $images_url/$js_url/$css_url become available and points to the url of the theme folders.


Layouts or NOT ?
There's no real need to use special layouts files to have your header/footer/sidebars , continuing with the above example, you can do something as follows:
Code:
$this->template
->set('partials_path','partials')
->add_partial(array('header'=>'header_file','footer'=>'footer_file','sidebar'=>'sidebar_file'))->load('welcome_message',$data);
//In this case, doing :
echo $header . $sidebar . $footer; in the welcome_message file would brings these files in.
With layouts :
Code:
$this->template->set('layouts_path','layouts')->set('layout','default')->load('welcome_message');
The above code, will look for a file in /views/website/layouts/default.php
which contains the $header/$footer/$sidebar which are loaded from partials AND $page_body which is actually the welcome_message file contents .

Right now we end up with a structure like :
Code:
-----/views
--------/website
------------/pages
--------------welcome_message.php
------------/css
------------/images
------------/js
------------/partials
--------------header_file.php
--------------footer_file.php
--------------sidebar_file.php
------------/layouts
--------------default.php
Which i believe it's pretty okay .
Also, if you are using HMVC/MS, and you use the above structure under a certain module and you want resources(images/js/css) to be loaded from there you can tell the library to do so and insteand of loading the images/css/js from /views/theme it will load from
/modules/module_name/views/theme

The library is pretty loose, you can organize your themes and their contents exactly how you want .

Managing Javascripts/Styleshets.
I have integrated options to use JSMin/CssMin + Phil's cache library for best performance.
A sample code :
Code:
$this->template
->set('group_js',TRUE)->set('minify_js',TRUE)->set('use_js_version',TRUE)
->add_js('script1.js?v=1')->add_js('script2.js?v=1')->add_js('script1000.js?v=1')
->add_js('script-with-php-inside.php?v=1');
In the above case, when loading the scripts, a string containing their names + their versions is created, then hashed with md5() and checked against the cache to see if it exists. If there is a cached version, that one will be served if not, the content of each file is read then all the files are groupped into one output writed to cache then served.
The generated url will be like
http://site.url/assets/js/long-md5-hash-...d-versions.
(Of course the assets controller needs to be created, but is very simple)
When using scripts version, the disadvantage is that after you modify a script, you must edit the version too.
If you pass set('use_js_version',FALSE) then instead of creating a md5() from the file names and versions, the md5 is created from the file contents therefore, everything is automated, and after you modify a script, the library will be self aware and will load the new version.The disadvantage is that, it takes time to read the files at every request.

Also you can minify and not group or group and not minify etc etc.
Same rules applies for stylesheets.

When loading scripts/styles in your controller, then in your views these would be available like
Code:
echo $page_scripts . $page_styles ;
The minify/groupping thing is something new and possible still needs some work, but for now i haven't encountered any problem and the bright side is that you can run php code inside without any problem though personally i don't really do it.


Some other aspects, the page titles are auto generated from controller/methods from the url, but also you can set it manually.You can add a title separator and pre/after title words , something like :
Code:
$this->template->set('before_title','My page is : ')
->set('title_separator','::')
->set('after_title','pretty cool huh ?');
// Results in :
My page is : Controller :: Method :: Param 1 pretty cool huh ?

Description/Keywords can be set also via :
Code:
$this->template->set('description','BLAH')->set('keywords','Blah');
//OR
$data['description'] = 'BLAH';
$data['keywords'] = 'BLAH';
$this->template->load('some_view',$data);

Plus, there are allot of features added (like a special config file with all the available config items or a special template helper or like any method accepts arrays or strings as params)
All the values passed via set() method could be also put in an array and initialized like :
Code:
$config = array('theme_path'=>'mytheme','pages_path'=>'pages'......[]);
$this->template->initialize($config);
//OR
$this->template->set($config);

So what do you believe about it ?

My only concern is that, maybe, using CssMin/JSMin is a bit too much for a template library and of course i am not sure how correct is to load resources like images/js/css from the views folder.
#2

[eluser]Twisted1919[/eluser]
Nobody ?
#3

[eluser]seanloving[/eluser]
I like that you don't have to use layouts if you don't want to. I just started using Phil's Template Library and I like it, but I think it might bug me that in every controller I have to set a layout and then build a view that is not a layout. How else is yours different than Phil's? It seems unusual that nobody ever made a comment here. Good luck.




Theme © iAndrew 2016 - Forum software by © MyBB