News:

Wondering if this will always be free?  See why free is better.

Main Menu

Using sub templates ..

Started by sangwe11, November 21, 2009, 07:26:14 PM

Previous topic - Next topic

sangwe11

I've always wondered how this is done, as when ever I do loadSubTemplate() in the sources file, it shows the sub template above everything else.

This isn't how I wanted it, and so I wondered what is the proper way to use it ?

Arantor

In the source file, set:
$context['sub_template'] = 'name_of_subtemplate';

Then when it's ready, template_name_of_subtemplate() will be loaded from the file from what loadTemplate was already executed.

E.g. loadTemplate('wiki');
$context['sub_template'] = 'edit_page';
Holder of controversial views, all of which my own.

sangwe11

Quote from: Arantor on November 21, 2009, 08:04:33 PM
In the source file, set:
$context['sub_template'] = 'name_of_subtemplate';

Then when it's ready, template_name_of_subtemplate() will be loaded from the file from what loadTemplate was already executed.

E.g. loadTemplate('wiki');
$context['sub_template'] = 'edit_page';

Hmm, it might just be me, but I didn't get that.

I got that I need to set a variable with the subtemplate name, but then it goes a little hazy.

I'm guessing that it automatically loads the subtemplate from the template file included, but what do I name the function, the same as I named in the subtemplate variable ?

Also, what happens if I want 2 or more subtemplates, and if they are in a different template file. (eg, header and footer might be in a general template file)

Arantor

Yeah, you put the name of the subtemplate function into the $context variable.

I should have finished the above with that it calls template_edit_page.

Generally you wouldn't have multiple subtemplates on a single page. It does happen, though, such as the poll template file being called from Display.php and Post.php, in which case you just call the template files individually from the subtemplate, like the BoardIndex template does to load the info center (which in 2.0 is a separate sub template)
Holder of controversial views, all of which my own.

sangwe11

Quote from: Arantor on November 21, 2009, 08:12:52 PM
Yeah, you put the name of the subtemplate function into the $context variable.

I should have finished the above with that it calls template_edit_page.

Generally you wouldn't have multiple subtemplates on a single page. It does happen, though, such as the poll template file being called from Display.php and Post.php, in which case you just call the template files individually from the subtemplate, like the BoardIndex template does to load the info center (which in 2.0 is a separate sub template)

Ahh, I got all of that except the multiple subtemplates.

Do I have to use the load subtemplate for that ?

And, another thing. When the subtemplate is loaded, how do I put it where I want ? Do I echo the variable I set in the designated place in the main template ?

Arantor

No, you load a single template, and call the other functions if you need to.

Like I pointed out, the BoardIndex template has two subtemplates.

BoardIndex itself primarily loads the BoardIndex.template.php and doesn't set a subtemplate, so it loads template_main, which is fine.

BoardIndex.template.php contains a second subtemplate, for the info centre, which is called manually at the end of template_main. Thus you only have one master subtemplate being used, and you call the other templates if you want them.

Remember, subtemplates are just regular functions that output stuff. You set up the data in $context that you want to display, the template displays the HTML you give it. If you want to ensure the order is right, make your template function display it all in order. If it branches off to another subtemplate, just make sure that you're calling it in the right place.
Holder of controversial views, all of which my own.

sangwe11

Quote from: Arantor on November 21, 2009, 08:24:04 PM
No, you load a single template, and call the other functions if you need to.

Like I pointed out, the BoardIndex template has two subtemplates.

BoardIndex itself primarily loads the BoardIndex.template.php and doesn't set a subtemplate, so it loads template_main, which is fine.

BoardIndex.template.php contains a second subtemplate, for the info centre, which is called manually at the end of template_main. Thus you only have one master subtemplate being used, and you call the other templates if you want them.

Remember, subtemplates are just regular functions that output stuff. You set up the data in $context that you want to display, the template displays the HTML you give it. If you want to ensure the order is right, make your template function display it all in order. If it branches off to another subtemplate, just make sure that you're calling it in the right place.

Ahh, I see.

Thanks!

Advertisement: