PDA

View Full Version : How to use compile_template function?


EWGF
05-08-2011, 12:43 AM
I want to parse conditions with a custom databasetable, so according to vb.com I need to use the compile_template() function.

/**
* Processes a raw template for conditionals, phrases etc into PHP code for eval()
*
* @param string Template
*
* @return string
*/
function compile_template($template, &$errors = array())
Besides including adminfunctions_template.php, I have NO idea how to use this in a .php file. Does anyone here know how to use this function?

Boofo
05-08-2011, 12:45 AM
I do on vb 4.

EWGF
05-09-2011, 12:15 PM
I've looked through some mods here on vb.org. Is it true you can only parse specific specified condition codes and not the template where you want to parse every possible code?

kh99
05-09-2011, 12:30 PM
I'm not sure what you're asking. There is a set of php functions that you are allowed to use in an if condtion, if that's what you mean. Are you saying you want to compile a template without that restriction?

EWGF
05-09-2011, 01:48 PM
Yes. I have made a simple cms -for outside the forum directory- with some custom tables, but the content in those custom tables can't use conditions and other php functions (the codes show up unparsed in the HTML output). According to vb.com I need to use the compile_template() function for this to work and I'm having trouble to get this function working.

Database content:
<if condition="$show[member]">Not viewable for guests</if>

HTML output getting unparsed:
<if condition="$show[member]">Not viewable for guests</if>

kh99
05-09-2011, 02:11 PM
I did this short test by creating a plugin using hook misc_start:

if ($_REQUEST['do'] == 'compile')
{
require_once("includes/adminfunctions_template.php");
$template = '<if condition="$show[member]">Not viewable for guests</if>';
echo compile_template($template);
exit;
}


and the result is:

".(($show[member]) ? ("Not viewable for guests") : (""))."


so it seems like it's working, although I'm not sure how you're supposed to deal with the dots at the beginning and end, I guess when the function is used there are other strings added to the beginning and end.

Anyway, are you using the return value of compile_template()? I don't think it changes the parameter you pass.

EWGF
05-09-2011, 09:12 PM
Yes. I also tried echoing and evaling the db->query or the template name.

But seeing your short test, it's not possible to process the custom db as a vBulletin template? If not, I might be able to wrap it up with some custom templates.

kh99
05-09-2011, 09:24 PM
Sorry, I don't really understand what you are asking.

But in case it helps, I played with it a little more and got this:

if ($_REQUEST['do'] == 'compile')
{
require_once("includes/adminfunctions_template.php");
$show[member] = true;
$template = '<if condition="$show[member]">Not viewable for guests</if>';
eval ('$output = "' . compile_template($template) . '";');
echo $output;
exit;
}


and the output is

Not viewable for guests


but maybe this has nothing to do with your problem.

Boofo
05-09-2011, 09:38 PM
Take a look at this mod. Bob uses compile_template for search and replace.

https://vborg.vbsupport.ru/misc.php?do=producthelp&pid=forumcategoryicons

EWGF
05-11-2011, 05:57 PM
Thanks, I will look in it
Sorry, I don't really understand what you are asking.

But in case it helps, I played with it a little more and got this:

if ($_REQUEST['do'] == 'compile')
{
require_once("includes/adminfunctions_template.php");
$show[member] = true;
$template = '<if condition="$show[member]">Not viewable for guests</if>';
eval ('$output = "' . compile_template($template) . '";');
echo $output;
exit;
}


and the output is

Not viewable for guests


but maybe this has nothing to do with your problem.
I tested it too, it doesn't work correct with an else statement though

<if condition="$show[member]">Not viewable for guests<else />Not viewable for members</if>
Not viewable for guestsNot viewable for members

I'm afraid there's no way in parsing a custom made database table as if it were a vB template

asdfadrian
05-15-2011, 02:32 AM
I have this issue as well. Maybe my contribution will provide some movement and a closer find to a fix.

I am using a custom template that is a cnp (which I plan to edit) of the FORUMDISPLAY template for specific forumids.

I discovered that the statement below does not work:
<if condition="$show['forumslist']">
However, if you replace the forumslist with quicksearch it will like below:
<if condition="$show['quicksearch']">

So some variables are not included in the template, so you have to define them somehow.

Further information and details can be found at this thread (https://vborg.vbsupport.ru/showthread.php?p=2195706#post2195706).

BirdOPrey5
05-16-2011, 08:39 PM
I know for me in VB3 templates I can never use the single quotes... try:
<if condition="$show[forumslist]">