Page 1 of 1

My template works, but I think I'm doing it the wrong way

Posted: 23 July 2021, 03:48
by JonChambers
The documentation is very clear on how to use a code block any number of times, and the technique works for any large number.

But it's not clear on the correct usage when the number can only range from 0 to 1.

The two player game is extremely unique, with a lot of HTML for the two player game. I need to show that HTML once in a 2 player game, or zero times in a 3-6 player game.

.tpl

Code: Select all

<!-- BEGIN game2p -->
//20 lines of HTML, including a nested code block with another code block inside that
<!-- END game2p -->
.view.php

Code: Select all

if($players_nbr == 2){
    $this->page->begin_block( "name_name", "innerBlock" );
    $this->page->begin_block( "name_name", "outerBlock" );
    $this->page->begin_block( "name_name", "game2p" );
    for($i = 1; $i <=4; $i++){
        $this->page->reset_subblocks( 'innerBlock' );
        for( $j=1; $j<=4; $j++ ) {
            $this->page->insert_block( 
                "innerBlock", array(
                    //args
                )
            );
        }
        $this->page->insert_block(
            "outerBlock", array(
                //args
            )
        );
    }
    $this->page->insert_block(
        "game2p", array()
    );
} else {
    $this->page->begin_block( "name_name", "game2p" );
}
So it all runs... fine. It does exactly what I intended it to do. But it feels... improper. Like anyone reading through my code would be left scratching their head saying "what is... oh I see, but why didn't you do it the NORMAL way?"

Is there a normal way? Or was that it?

The BGA documentation is literally the only thing I know about .tpl files. I know absolutely nothing about them outside of what's written there.

Thanks in advance

Edit:

Attempt 2, not sure if the code just got more proper or less proper:

.view.php

Code: Select all

$this->page->begin_block( "name_name", "innerBlock" );
$this->page->begin_block( "name_name", "outerBlock" );
$this->page->begin_block( "name_name", "game2p" );
if($players_nbr == 2){
    for($i = 1; $i <=4; $i++){
        $this->page->reset_subblocks( 'innerBlock' );
        for( $j=1; $j<=4; $j++ ) {
            $this->page->insert_block( 
                "innerBlock", array(
                    //args
                )
            );
        }
        $this->page->insert_block(
            "outerBlock", array(
                //args
            )
        );
    }
    $this->page->insert_block(
        "game2p", array()
    );
} else {
    //3+ player code
}

Re: My template works, but I think I'm doing it the wrong way

Posted: 23 July 2021, 09:46
by Tisaac
Tpl is a very old (~2010) way of doing php templating, and it's hard to find documentation on that.
That's why I almost never use view.php and instead construct my layout in js.
If you need some kind of "if" in tpl, I found (by experimenting) that this approach works pretty well (meaning no extra html is sent by server) :

Code: Select all

if($this->game->getGameStateValue('optionHint') == FREE){
      $this->page->unset_var("snapped");
      $this->page->insert_block("free", []);
    } else {
      $this->page->unset_var("free");
      $this->page->insert_block("snapped", []);
 }

Code: Select all

<!-- BEGIN snapped -->
My tpl for snapped stuff
<!-- END snapped -->


<!-- BEGIN free -->
My tpl for free stuff
<!-- END free -->
I would guess the trick (unset_var) should also work for your example.