Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Views: Twig or no Twig?
03-11-2014, 02:15 PM, (This post was last modified: 03-11-2014, 08:46 PM by icedlava.)
#5
RE: Views: Twig or no Twig?
I found twig took a short while to get used to - but much better than any of the other heavy weight templating engines I've used.

One would expect to have variables embedded in the HTML in twig because that is what template systems do - they allow you to separate out HTML from the PHP code and thus provide a basis for much easier templating of themes. Theme creaters in general do not need to know PHP. Rendering of output is separate from code.

On the plus side too, Twig is very easy to integrate to webERP - incredibly so and comes with a nice caching system. Initial results i had demonstrate performance improvement. Other benefits are easy PDF output etc.

The best part is one can just change the view without having to touch the php code files.

PHP has changed a lot over the years since initial versions, as have templating systems, caching options and so on. It takes some time to throw off old habits and learn new ones - sometimes it's worthwhlle and in the case of TWIG i've found it to be so.

Cheers,

I forgot to add that the code files in webERP - once the HTML output is removed, are very much smaller and easier to read (at least for me). I also found i could remove a lot of duplicate code. That's another plus I guess.
One of the things about TWIG is that it does force you to relook at the code and what variables you need to pass to the output. The idea I've kept in mind is to attempt to reduce any use of PHP or non HTML code in the templates when deciding what variables to pass to twig output.

For those interested a quick and dirty integration with webERP goes like so (been a while since i did this so i'm looking at my rough notes from months ago) - it was just to test it out.

1. Create a directory called 'twig' in the includes directory of webERP.
2. Download twig files and put these files in the new includes/twig directory
3. Create a directory 'output' in the html directory writable by your web server - this is where templates can be stored
4. Create a directory 'tmp' in the html directory writeable by your webserver - this is where cache output is stored
5. I made a new includes/header.php and includes/footer.php file to take the place of header.inc and footer.inc. You could just use the existing header.inc and footer.inc with changes but the new header.php and footer.php add some flexibility.
In the new header.php the key lines are:
Code:
//template system loaded in the header file
    require_once './includes/twig/lib/Twig/Autoloader.php';
    Twig_Autoloader::register();
    $loader = new Twig_Loader_Filesystem(array('./output/templates','./output/templates/includes'));
    $twig = new Twig_Environment($loader, array(
            'cache' => './tmp/cache',
            'debug' => true,
            'auto_reload' => true
    ));
    //template system done
You could then also add here an array containing all the variables you want to output to the header area in the templates eg
Code:
$params =
        array(
    'StrictXHTML'     => $StrictXHTML,
    'SessionTheme'    => $_SESSION['Theme'],
    'SessionLanguage' => $_SESSION['Language',
...
etc,
'Logout' => _('Logout')
);
In the new footer all I have are the parameters to send to the template
Code:
$params = array_merge ($params, array(
    'version' => _('version'),
    'versionNo' =>  $_SESSION['VersionNumber'],
    'copyright' =>  _('Copyright'),
    'year'      => Date('Y'),
    'logofile' => $_SESSION['LogoFile'],
    'RootPath' => $RootPath,
    'datetime' => DisplayDateTime()
    )

6. Create your templates and put them in the directory specified in point 5 above which is in my case output/templates or output/templates/includes if i template one of the include files.

7. In each webERP script file, make sure to include header.php and footer.php, capture the variables to send to the template in an array, and send to the template:
Code:
$params = array_merge($params, array(
   'StatusMsg' => $statusMsg,
   'RootPath'   => $RootPath,
....
   'assets' => $assets,
   )
);

//set the template to use and output!
    $template = $twig->loadTemplate('FixedAssetCategories.phtml');
    $template->display($params);

I used array_merge above - because i had a $params previously defined in header template. We could architecture a better integration but this was nice and quick for a look see.

That's about it for a quick and dirty integration with caching.

I might have forgotten something but serakfalcon could let me know as he's working with it now Smile

I started templating some simple files to get started and moved on to more complex ones. As I went I found I could tidy up the webERP code and ensure that as much processing is done before output to the templates - this then minimizes the processing there and keeps the HTML looking more like HTML, easier for themers too!

Cheers,


PS. thanks to serakfalcon for the TWIG thread - good to have somewhere to discuss it!

Reply


Messages In This Thread
Views: Twig or no Twig? - by serakfalcon - 03-11-2014, 02:22 AM
RE: Views: Twig or no Twig? - by Forums - 03-11-2014, 03:03 AM
RE: Views: Twig or no Twig? - by agaluski - 03-11-2014, 03:17 AM
RE: Views: Twig or no Twig? - by serakfalcon - 03-11-2014, 12:56 PM
RE: Views: Twig or no Twig? - by icedlava - 03-11-2014, 02:15 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)