Categories > TinyButStrong general >

sub-template & php include...

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: lauren
Date: 2012-07-20
Time: 13:46

sub-template & php include...

hi :)

i have a template for my page headers common to all pages called "header.tpl" ... in this header.tpl file i now need to draw a navigation menu that has top-level items with optional dropdown sub-menus

i think i should be able to do this with sub-blocks and 2 queries linked by the '%p1%' thing IF it was on a normal page

i'm getting confused as to where i include the php lines that do the 2 $TBS->MergeBlock(blah,blah,sql) statements, since included tpl files don't have the normal LoadTemplate() mechanism

am i approaching this wrong?

[edit]
so if i put the 2 $TBS->MergeBlock() statements in each php file that needs the menu drawn it works but this seems really not a good way to do things - surely there is a way to include some common code with an included template no?
[/edit]

By: Skrol29
Date: 2012-07-21
Time: 00:58

Re: sub-template & php include...

Hi Lauren,

There is a feature for this kind of difficulties.

In your sub-template "header.tpl" you can add a "template variable". Like this : [onload;tplvars;header=1]
As soon as the sub-template is included in the main template, then it feed the property TplVars, so you have: $TBS->TplVars['header']='1';
You can use this key in your main code, in order to check if your have the sub-template or not. If it's so then merge-it.

The other way is to include a sub-template as a PHP script (parameter "script") and have the script driving the merging of its sub-part.
By: lauren
Date: 2012-07-25
Time: 17:42

Re: sub-template & php include...

thanks for getting back to me

i have read and re-read your answer, and looked at the manual a few times but still cannot quite understand your answer

currently i have to paste 2 MergeBlock calls into each page for the menu to appear, and now i am going to have to add another 2 or 3 for the various sidebar widgets that need to be merged from the database

is there no simpler way to do this? like, in the same way we have a .TPL file and a .PHP file, is it possible to have a .PHP file for a sub-template? does this require manual merging into a veriable on the main .PHP file and then merged with the onshow? or am i completely off-base in my thinking?

thanks again for the help
By: Anonymous
Date: 2012-07-31
Time: 01:01

Re: sub-template & php include...

Help me understand something about what you are doing... when you say 'MergeBlock for menus' is that becasue you are populating the menus from a data array or a db query?

If you are concerned that menus may (probably will) change in the future - and you don't want to have to edit every page to make the changes - then consider making a static html for a menu, then do a simple
[onload;file='menu-1.html';]
in each of your site's pages.
then you only ever need to edit the menu-1.html file.
I have one master template with my headers, sidebar menus & db widgets, and footer -- with the center block unspecified. Then I use this master template as the beginnings for each of the individual pages in the site.

If some of your stock items are 'widgets' based on db query results then use the also simple
[onload.file;script=specialbox.php;subtpl]
where the script  specialbox.php is a php script that runs TBS and calls it's own template file

Just try these trick to get them working in trivial cases to see the inner workings

BTW -- here's a code snippet from Skrol29 it belongs at the top of EVERY TBS php file - both  the main files and the subtemplate scripts
//include your TBS class here
include_once("/abs_path/script_to_include_tbsclass_dbhandler_plugins.php");

// ======== thanks Skrol29  ==========
if (isset($this)) {
    // We are under the TBS Subtemplate Mode =>
    //   variables are always local, not global,
    //   and the TBS object is referenced by
        //     variable $this.
    $TBS =& $this;
        $TBS->NoErr = true;
} else {
      // subscript can be run under normal mode =>
      // corresponding template will be displayed
        // like a main template.
        // include_once('tbs_class.php') ;
    $TBS = new clsTinyButStrong;
        $TBS->NoErr = true;
}
Using this snippet insures that your master and subtemplate code will work.

Cheers, and thanks for TBS every day
TomH
By: lauren
Date: 2012-08-03
Time: 11:36

Re: sub-template & php include...

tom

thanks for jumping in and trying to explain something i think i'm just not getting :)

the situation is that in the CMS they can add pages dynamically and assign them to any given dropdown menu, so the menus have to be fetched from the db each time they are drawn

i think your final suggestion is the way to go but i will have to study it further before i know for sure
thanks again tho
:)
By: TomH
Date: 2012-08-03
Time: 23:24

Re: sub-template & php include...

One additional thought...

I understand your pain exactly. For fun I wrote a CMS in TBS and all content and various menus (many different categories) are all db driven.

In situations like dynamic content for checkbox lists on forms, changing content in menus/submenus consider the frequency at which the content changes. If it is db content - but changes not too often. one of the things i do regularly is to write a TBS script that reads the db and creates a static HTML file -- I then include the static HTML file into the pages (saves lots of db thrashing since these things are on every page).  I  run the TBS script  that makes the static files via a cron job automatically as often as needed. Even if you run the cron every 10/15 minutes it's still probably a lot less server/db load then having those menus created for every page load.

Keep asking for help on this forum - the wizards here can help in many ways.

Cheers.
TomH