The main template
The main template is the template that is being used for rendering the whole page. By default this is one file with mixed PHP and HTML and it is defined in tPy::$conf['template']. This can be changed to your needs. You can also split it into parts like footer and header if needed. Bascially all we do is include this template and output the fields of the current objects as described below.
tPy::$obj and tPy::$obj_ref
These vars are most important and usually set by pub/index.php. Check out the following example:
tPy::$conf['tPy_db']['page'] holds the db reference (in this case }}pages}) and tPy_url is a constant that holds the current url relative to the installation folder. Since we save pages by default in the db with the relative url that is all we need to load a page by default. This way you get a maximum of flexibilty and performance as well. tPy::$obj_ref needs to be set with the correct path in order to make the extended frontend work properly. Until the template is included you can alter the vars as you wish if necessary. This is a simplified version, see the examples pub/index.php and templates/template.php for details.
While the template looks similar to this:
tPy::$obj_ref = tPy::$conf['tPy_db']['page'] . tPy_url;
tPy::$obj = tPy_db::doc(tPy::$obj_ref);
You can also change the template if you set tPy::$conf['template'] to a new value. This can also be done on the go depending on your needs in the bootstrap pub/index.php for example. The <!-- ###CLIENT_INCLUDES### --> will output a string with a reference to all included files and <?= tPy::$frontend ?> holds the frontend for the current user. The base href tag is necessary in order to make tPy work in subfolders. The rest is pretty much outputting the fields from the database that are loaded in tPy::$obj.
<title><?= @tPy::$obj['title'] ?></title>
<base href="<?= tPy_base_url ?>" id="tPy_base_url" />
<?= @tPy::$obj['head'] ?>
<!-- ###CLIENT_INCLUDES### -->
<?= tPy::$ext_frontend ?>
<div id="tPy_obj[content]" class="tPe_column" data-column="content" data-default_element_width="3">
<?= @tPy::$obj['content'] ?>
Working with views
Often you need work with views, meaning small portions of data for templating. On the server you can use this method:
The path should be relative to the tPy_site_dir - so for example if you want to include a view in the ext folder you would include it like this:
$views = tPy::getView('ext/my_extension/views.php');
The view file can contain of different view parts like this (this is an excerpt from the tPy_login extension):
Now you can access the register view with $view['register']. The second optional $data argument in tPy::getView can be used for associative arrays with data - these are directly accessible then. For example tPy::getView('ext/tPy_login/view.php', ['var1' => 'foo']) will make $var1 accessible.
<form id="login-form" data-onsubmit="return false">
<?= tPy_login::$translate['your_email'] ?><br>
<input type="text" name="login" id="login" value="" class="tPy_login_input tPy_login_default_width" autocorrect="off" autocapitalize="off">
<?= tPy_login::$translate['password'] ?><br>
<input type="password" name="pw" id="pw" class="tPy_login_input tPy_login_default_width"><br>
<button class="tPy_login_input" data-onclick="tPy_login.login">
<?= tPy_login::$translate['login_button'] ?>
<a href="<?= tPy_url ?>?action=register" style="margin-left:20px;float:right;color:black;"><?= tPy_login::$translate['register_now'] ?></a>