Let's say we have the template viewtopic_body.html with the following code:
and the template viewtopic_post.html contains this (example reduced to the minimum):
the output will be empty. Why? Because the template system creates the following PHP code for the loop:
To be precise: the "loop data" is assigned to a local variable, called $_postrow_val. However, the inclusion of the other template is done with a class method. It's rendered PHP code would be:
...but $_postrow_val does not exist here anymore. It also can't be accessed using the global keyword (or would require using it at even more places due to variable scopes in PHP).
A solution to this would be to not use local variables anymore, but class properties. The assignment of the loop code would then look like this:
and so in the included template a code like this will also succeed in accessing existing data:
I tested this successfully and it would also solve a lot of problems using template includes. Likewise, templates could be unified (displaying private messages and displaying posts could use the same user details template code because they could use a template inclusion).
For PHP's variable scope across objects there are two comments which demonstrate the problem quite well: