TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

Wrong order of template content after show ?!?

By: Fabrizio Lodi
Date: 2017-02-13
Time: 15:06

Wrong order of template content after show ?!?

I create my final template through multiple "LoadTemplate" commands (all except the first with a "+" instead of the charset).

Let's say I load a "head.tm", then a "main.htm" and finally a "tail.htm".

Both head and tail templates uses a custom tag linked to a function

$TBS->MergeField('include','getInclude',true);

to include css or js content with a programmatic constructed file name, checking before that the file exist:

function getInclude($type,$params) {

... $file is defined by reasoning, and check that it exists ...

    $c="<$tag>\n";
    $c.=file_get_contents($file);
    $c.="</$tag>\n";
    $app["TBS"]->Source.=$c;
    $app["TBS"]->LoadTemplate(null);
}

I use the tag 2 times, one at the end of "head.htm":

[include.css;mode=app]

And one in "tail.htm":

[include.js;mode=app]

All works great, the only things is that in the final page I get both the files where in "tail.htm" I call the function, and not one ad the end of "head.htm" and one in "tail.htm" as expected.

Any idea?

Thanks!!!
By: Skrol29
Date: 2017-02-16
Time: 09:39

Re: Wrong order of template content after show ?!?

Hi  Fabrizio,

The point is that your function getInclude() should not drive the template. It should only return the result of the content for the field.
So there should be no « $app["TBS"]->Source.=$c; » nor « $app["TBS"]->LoadTemplate(null); ».
The double content you've got is because of that: inserted once by the function, and once by TBS.

Considering your previous post, I guess you're doing this for merging fields in your sub-templates.

For inserting your JS ans CSS contents, did you try with TBS sub-templates ?
Something like this :
<script type="text/javascript">[include.js;file=[val];magnet=script]</script>
This will insert the sub-template, merge [onload] fields in the sub-template. It will delete the <script> entity if value of "js" is empty, thanks to parameter "magnet".

If you actually need to drive the merging of sub-template, then it is better to use parameter "onformat" with parameter "subtpl".
This enables you to call a PHP function where the scope of TBS is only for the sub-template.

See:
http://www.tinybutstrong.com/manual.php#html_subtemplate
http://www.tinybutstrong.com/manual.php#html_field_prm_file
http://www.tinybutstrong.com/manual.php#html_field_prm_subtpl