PDA

View Full Version : [How-To] Use Template Conditional in any Script


harmor19
01 Jun 2007, 23:05
This tutorial will show you how to return data from a column you've created and parse template conditionals.


<?php

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'my_script');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array();

// pre-cache templates used by specific actions
$actiontemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

//You must include this in order to parse HTML based conditionals
require_once(DIR . '/includes/adminfunctions_template.php');
// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

//First we need to query the table.
$sql = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "my_table WHERE column = 'X' ");

//Now we need to convert the HTML based conditionals to PHP based
eval('$done = "' . compile_template($sql['my_field']) . '" ;');

//Print the results to the screen
eval('print_output("' . fetch_template('my template') . '");');

?>




For sake of this article let's say "$sql['my_field']" contains the following
<if condition="is_member_of($bbuseruserinfo, 12)">
To access the arcade click on "Links" to access the menu.<br />
Within the menu choose "Arcade".
<else />
<if condition="!is_member_of($bbuseruserinfo, 12)">
To access the arcade you need a minimum of 1000 posts. Currently you only have $bbuserinfo[posts]
</if>
</if>




You'll need to take the variable "$done" and place it in "my_template".

$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
$headinclude
<title>$vboptions[bbtitle]</title>
</head>
<body>

$header
$navbar

<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%"

align="center">
<tr>
<td class="tcat">My Custom Page Title</td>
</tr>
<tr>
<td class="alt1">$done</td>
</tr>
</table>

$footer
</body>
</html>



View the attachment to see what the end result.


I have tested the security and it returns dangerous code as plain text. This means that no one can perform mischievous actions towards your forums.

Princeton
02 Jun 2007, 16:11
Can you explain in detail WHY you would want to create such a tool?

Many members won't understand; therefore, limiting the help that you are trying to achieve.

harmor19
02 Jun 2007, 17:57
I'm sorry Princeton but I'm not good at explaining how stuff works.

Antivirus
03 Jun 2007, 17:59
I would have never thought of using a template conditinal in that manner, very interesting. Thanks!

RedTyger
25 Jul 2007, 11:52
I had no idea this could be done, which is ridiculous since it's perfectly obvious now it's been pointed out. Thank you, I've made good use of it.