bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

Switch GUI directory via f3:include attribute #145

Closed domwebb closed 11 years ago

domwebb commented 11 years ago

I need to be able to switch between two directories within templates. In the below code the second include needs to switch the GUI directory to '../app/view/', and then we need to go back to the globaly set directory path:

<f3:include href="common/header.htm"  />
<f3:include href="ecommerce/view-basket.htm" gui="../app/view/" />
<f3:include href="common/footer.htm" />

As you can see I have added an extra attribute to the f3:include tag.

Iv altered the template.php to store the globally set value, temporary set the global value of GUI to the value of 'gui' attribute and then set it back to the temporary value.

  1. Can this be a feature in future releases
  2. Is:

    function build($node) { $out=''; $tempGuiOld = F3::get('GUI');

    ..... if (isset($nval['@attrib']['gui'])) { F3::set('GUI', $nval['@attrib']['gui']); } .... F3::set('GUI', $tempGuiOld);

the best way to do this?

stevewasiura commented 11 years ago

can you list your folder structure for each GUI so we get an idea of where this alternate template would be located?

you can also use multiple settings in the GUI var during F3 config, and F3 will search those various GUI values until it finds a match for the file name. would that work for you?

domwebb commented 11 years ago

The search and match wont work (in my use case) as we are looking to include a common file. Essentially we are providing multiple sites (site1, site2 etc in the ~/sites folder) from one codebase/install (~/app). Certain elements such as checkout, basket, customer login etc are common html that can be reused.

In our index.php we do:

$w = new Axon('website');
$serverName = str_replace('www.', '', $_SERVER['SERVER_NAME']);
$w->load(array('base_uri = :bu', array('bu'=>$serverName)));
.....
F3::set('GUI', '../sites/'.$w->base_path.'/view/');

to get the correct 'view' folder and set things up for the Template::serve() call later on. The header and footer calls (1st and 3rd f3:include), need to be served from the 'site' specific folder.

f3:include href="common/header.htm" />
f3:include href="ecommerce/view-basket.htm" gui="../app/view/" />
f3:include href="common/footer.htm" />

The structure is:

To be honest this works for us as a solution. Its 10 lines of code added to one file, or something like that - trivial is my point. I figure this must be something else people come up against? In the greater good I figured asking may be a good idea.

Dom

stevewasiura commented 11 years ago

glad it works with your modification, but it's hacking core, if it it is not accepted by Bong, then applying future updates of the framework will overwrite your modification(s).

have you tried to add a second path to your initial GUI var setter separated by a comma (,) semi-colon (;) or pipe symbol (|) ?

F3::set('GUI', '../sites/'.$w->base_path.'/view/' . '|../app/view/'   );

and comment out your custom code that resets the GUI value?

if you try that and you get an error, please paste the msg?

also since your view folder will contain more than one, perhaps you might rename it "views" (plural)

bcosca commented 11 years ago

Why not:

f3:include href="../app/view/ecommerce/view-basket.htm" />

All href paths are relative to your GUI setting.

KOTRET commented 11 years ago

the solution offered by @stevewasiura looks good as you have 2 GUI-folders: common and site-related. this way youcan access the templates as if they were in your gui-folder, dont need a placeholder in each f3:include-statement and there is no chance to take the template of another site by accident. Beware of identical template paths between common and site only.

This way you also can move the site-templates anywhere without updating all F3:include in templates.

eazuka commented 11 years ago

i really don't see any reason why this can not be done as bong has suggested.

why: gui="../app/view/"

when you can just do it directly like:

f3:include href="../app/view/ecommerce/view-basket.htm" />

stevewasiura commented 11 years ago

thanks Chris @KOTRET

bcosca commented 11 years ago

No feedback from OP for quite some time. Closing.