Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: ivimendoza - kesäkuu 09, 2014, 05:31:26 AP

Otsikko: Understanding the process behind templates
Kirjoitti: ivimendoza - kesäkuu 09, 2014, 05:31:26 AP
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.
Otsikko: Re: Understanding the process behind templates
Kirjoitti: Arantor - kesäkuu 09, 2014, 06:01:23 AP
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.
Otsikko: Re: Understanding the process behind templates
Kirjoitti: Bloc - kesäkuu 09, 2014, 06:34:03 AP
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.

Otsikko: Re: Understanding the process behind templates
Kirjoitti: ivimendoza - kesäkuu 09, 2014, 11:20:27 AP
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!
Otsikko: Re: Understanding the process behind templates
Kirjoitti: Arantor - kesäkuu 09, 2014, 11:24:02 AP
I thought I already explained this. loadSubTemplate() is the one that calls them.
Otsikko: Re: Understanding the process behind templates
Kirjoitti: Angelina Belle - kesäkuu 09, 2014, 11:58:32 AP
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.


Otsikko: Re: Understanding the process behind templates
Kirjoitti: ivimendoza - kesäkuu 09, 2014, 12:23:57 IP
Lainaus käyttäjältä: angelinabelle - kesäkuu 09, 2014, 11:58:32 AP
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 :)
Otsikko: Re: Understanding the process behind templates
Kirjoitti: Angelina Belle - kesäkuu 09, 2014, 01:16:04 IP
Ther is still loads more to learn about php.  Enjoy!