Understanding the process behind templates

Started by ivimendoza, June 09, 2014, 05:31:26 AM

Previous topic - Next topic

ivimendoza

Hello there,

I've been searching in the documentation, but I found no answer, so that's the reason I write you guys, here. Maybe I could find this somewhere, but it's really confusing for me anyways. Here goes the question:

I've been working with SMF many years, but didn't noticed about how the templates works. I mean, when I go, for example, to ManageMaintenance.template.php, I find functions like this, which defines the template: template_maintain_database(), template_maintain_routine(). I've been looking where does that functions executes to, but I could'nt really find it. My question is: how does SMF auto-execute all these functions?

Thanks.

Arantor

There's no actual magic.

At some point, $context['sub_template'] will be set by a part of the controller (the action doing the work) to reflect the name of the template to be called, though it defaults to main, which means to call template_main.

The maintenance code, and some other places, set this based on the area requested rather than a discrete string; ManageMaintenance.php defines this in ManageMaintenance() itself based on the areas defined for the maintenance section, for example.

As far as running that is concerned, just about every branch of execution ultimately results in a call to obExit(), which goes through and calls loadSubTemplate() which actually executes the relevant code.

Bloc

Arantor beat me to it.. :) but the short version if you need it:

Setting the $context['sub_template'] variable execute a subtemplate like those you mention. For ManageMaintenace.template.php that would be Sources/ManageMaintenace.php. Look for it in that file.


There are also "layers", which call either a "template_main" or "template_xxx_above" + "template_xxx_below" - in case you are looking a bit deeper into the theme functions.


ivimendoza

Hello guys,

Thanks for your answers. I've reviewed it. In fact, I understand what $context['sub_template'] is supposed to do, but I still don't understand how it access to all these actions without execute it. I mean:

I usually execute a function like this:

function blabla(){
echo "hello";
}

blabla();

And I get the output. But I've searched a bunch of functions within different template pages, and didn't find it the execution anywhere.

This is a bit confusing for me.

Thanks!

Arantor

I thought I already explained this. loadSubTemplate() is the one that calls them.

Angelina Belle

Perhaps you are not familiar with a function name being stored as a string, so that the function can be called using that variable name.

Look for $theme_function() in loadSubTemplate.  See where loadSubTemplate is called.


Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

ivimendoza

Quote from: angelinabelle on June 09, 2014, 11:58:32 AM
Perhaps you are not familiar with a function name being stored as a string, so that the function can be called using that variable name.

Look for $theme_function() in loadSubTemplate.  See where loadSubTemplate is called.

Ahh! I catch it now! All seems pretty logical. Thank you all guys! Question solved :)

Angelina Belle

Ther is still loads more to learn about php.  Enjoy!
Never attribute to malice that which is adequately explained by stupidity. -- Hanlon's Razor

Advertisement: