8b. Working with BEGINSECTION (2)

This part covers a more complex usage of BEGINSECTION.

As mentioned, you can use SECTION with BLOCK & IF, this is how you do that.


In section.tpl:

<!-- BEGINSECTION justtest -->
<table border="1" width="100%">
<!-- BEGINIF $login -->
<tr><td style="background:green">Logged In</td></tr>
<!-- ELSE -->
<tr><td style="background:red">Not Logged</td></tr>
<!-- ENDIF -->

In test.tpl:

<p>Welcome to test page!</p>

<h1>Test 1</h1>

<h1>Test 2</h1>

In test.php:

require './includes/user_init.php';

// load the required section (in section.tpl)
// usually you don't need this, as qE load section.tpl automatically
load_section ('section.tpl');

// now load the output tpl
$tpl = load_tpl ('test.tpl');

// then create the sample data
$login = false;
$txt['table1'] = quick_tpl (load_tpl ('var', $tpl_section['justtest']), $txt);

$login = true;
$txt['table2'] = quick_tpl (load_tpl ('var', $tpl_section['justtest']), $txt);

// output
$txt['main_body'] = quick_tpl ($tpl, $txt);
flush_tpl ();


The important part is:

$txt['table1'] = quick_tpl (load_tpl ('var', $tpl_section['justtest']), $txt);

You have already know load_tpl, now we have load_tpl ('var', var). load_tpl ('var', var) is used to load a template from a variable instead of a tpl file. So using the above line, qE loads template from $tpl_section['justtest'] variable, which is defined in section.tpl.

By loading the tpl from a variable, you can easily manipulate the output. In this example, you can use the same SECTION to display the result according to $login status (by using BEGINIF).

There is no comment. Why not be the first?

More Comments/Post Your Own