Invenzzia »

Pages: [1]   Go Down
  Print  
Author Topic: OPTv2: Multiple Template Directories  (Read 1182 times)
Description: How to use multiple template directories?
0 Members and 2 Guests are viewing this topic.
duclet
User

Offline Offline

Posts: 8


View Profile
« on: April 25, 2009, 16:56:45 »

I will be starting work on a new project and I want to try to use OPT as the template system for it. Since it is modular, I want the template of each module to be in a template folder inside of its own folder while still using the same templates_c folder. What is the best way to do this?
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #1 on: April 26, 2009, 06:21:23 »

Hi, currently there are two ways:

1. In sourceDir you can define more than one path as an assotiative array. You could register there paths for your modules under their unique name (i.e. "news" or "articles") and then choose the template names with this module prefix: news:template1.tpl etc. templates_c remains the same for all the modules and OPT pays attention not to overwrite any compiled template by accident. You could also try to use stdStream configuration option to select the default template path, but in currently released versions, this would require a bit help on the template side. In the near future, there should appear a kind of simplification that I've invented.

2. I had a quite similar problem some time ago, but with OPT 1.x. I simply extended Opt_Class and wrote a new method to locate templates on the filesystem. It gives better results, but it is harder to achieve, as you have to hack some internal OPT systems.

There are four methods that are used to create and check the paths:

- Opt_Class::_stream() - produces a path to the original template.
- Opt_View::_convert() - produces a path to the compiled template.
- Opt_View::_preprocess() - checks if the template exists on the filesystem and needs to be recompiled (it uses _stream() and _convert()).
- Opt_View::_massPreprocess() - like above, but this is the version for the template inheritance that checks a list of templates at once to reduce the number of disk operations.

In the basic version, just _stream() and _convert() need to be changed to look for the template in more than one directory automatically, and to provide a unique file name within templates_c/ directory. You have to remember that in CM_PERFORMANCE mode OPT uses only the _convert() method, if you are going to use it, you have provide a support for this case.

The more extended version affects also the last two methods in order not to perform unnecessary disk operations.

In general, I see I must provide some dedicated solution for this problem, because you are not the first person asking for it :).
Logged

PozDrX, Zyx
---Invenzzia group---
duclet
User

Offline Offline

Posts: 8


View Profile
« Reply #2 on: April 26, 2009, 15:53:12 »

I think I like the first option better, mainly because it is easier to implement. But yeah, since I will be creating some kind of wrapper methods anymore, I can make sure it contains the right prefix. Plus, the more that I think about the prefix, I actually really liked the idea. I'll give this a test and see how it goes. Thanks for the help.

Also, another question since I have you, is there a way to process the template directly from a String instead of a file? I was planning on having some of the templates stored in the database and would like to process them right away rather than writing to a file and then process.
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #3 on: April 26, 2009, 17:00:55 »

To your second question -> you can achieve this with PHP streams (this is why OPT does not have any dedicated interface for this, like Smarty). You simply write a new PHP stream and you can work with it using ordinary filesystem functions. An example can be found in OPT unit tests (the /tests) directory. The tests are simple text files that contain: the template, the optional PHP code and expected result. In /tests/include/filesystemWrapper.php you can find a PHP stream implementation that reads these files and serves them as a fake filesystem for OPT.
Logged

PozDrX, Zyx
---Invenzzia group---
Pages: [1]   Go Up
  Print  
 
Jump to:  

Subject Started by Replies Views Last post
OPTv2: kod śledzenia Google Analytics poliman 3 533 Last post December 29, 2009, 19:48:00
by eXtreme
OPTv2: Widoczny prolog w trybie debug i blad wyswietlania strony bez niego. gorky_park 1 598 Last post October 30, 2009, 11:39:13
by Zyx
OPL for Zend Framework ersonic 1 274 Last post March 09, 2011, 21:27:24
by Zyx
OPTv2 : Naprzemienne kolorowanie rekordów El_Darro 7 1808 Last post May 26, 2008, 13:55:07
by megaweb
OPTv2: Znak   zamieniany na krzaczki. mrKrecik 12 4729 Last post November 19, 2010, 08:11:24
by Zyx