2

I'm trying to make custom pages using the new vBulletin 4.

My PHP file uses the code below:

$templater = vB_Template::create('TEST');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());

My template looks like this:

{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
  <head>
    <title>{vb:raw vboptions.bbtitle} - {vb:raw pagetitle}</title>
    {vb:raw headinclude}
    {vb:raw headinclude_bottom}
  </head>
  <body>

    {vb:raw header}

    {vb:raw navbar}

    <div id="pagetitle">
      <h1>{vb:raw pagetitle}</h1>
    </div>

    <h2 class="blockhead">Title</h2>
    <div class="blockbody">
      <div class="blockrow">
        Text
      </div>
    </div>

    {vb:raw footer}
  </body>
</html>

My question relates to the use of one single template for around 50 pages, which used HTML and PHP together and were working in vBulletin 3.

Will I need to use a separate template for each custom page that I make from the existing pages?

EDIT 1: I've opened a BOUNTY worth 100 points for this question. If you need any more details on my question, please leave a comment.

EDIT 2: Originally, I was using eval('$mytemplate = "' . fetch_template('mytemplate') . '";'); using vB3. This has broken the pages in vB4. If I follow ARandomOWI's post, will I be following the best approach?

2 Answers 2

3
+50

You can use the same template for all your custom pages if you only want to change some of the content between them.

In your PHP file you can pass variables to your template using:

$templater->register('var_name_used_in_template', $your_php_var);

This needs to be included before the "print_output" line. Then in your template you can add:

{vb:var var_name_used_in_template}

To display the contents of the variable "$your_php_var" that existed in the PHP.

This way you can use "$your_php_var" to store some HTML to be displayed within your template. You can split the content between multiple PHP files or use a single file for all the content and use PHP conditionals to alter "$your_php_var".

Note: variables passed with "vb:var" are first put through the function "htmlspecialchars_uni()". If this causes problems, you can use "vb:raw" instead which passes the raw variable. Although, be sure that the variable has already been sanitized within the PHP file.

Hope that's clear enough.

8
  • @ARandomOWl: Thanks for your answer. I have a lot of HTML code that is unique to each file. How do I avoid the trouble of escaping characters to put them in the PHP string? Mar 13, 2011 at 19:36
  • 1
    @Jon You could either create separate templates for your HTML or use the following in the PHP file: <? ob_start(); ?> HTML here <? $your_php_var = ob_get_flush(); ?> Just paste your HTML in there as normal. The output will be buffered and then stored in the variable. More info here: link
    – ARandomOWl
    Mar 13, 2011 at 22:12
  • @ARandomOWl: Using ob_get_flush(); helps. I also had to use ob_end_clean(); after it. Mar 14, 2011 at 5:10
  • @ARandomOWI: I've added a small question in my original post. Thanks for your help! Mar 16, 2011 at 8:05
  • @ARandomOWI:When I use this method, the value of the action attribute in my form gets overwritten. What could be the reason? Mar 18, 2011 at 17:34
2

@Jon: If you already have all the templates and PHP files setup from the VB3 version, you can replace the

eval('$mytemplate = "' . fetch_template('mytemplate') . '";');

with

$templater = vB_Template::create('mytemplate');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());

If I understand correctly. You may wish to alter the "pagetitle" and "navbar" variables. How you go about it depends on what you are working with. (Whether you are trying to make existing code work, or start from scratch.)

2
  • @ARandomOWI: I think the first answer will help, since I don't have templates for each of the pages. The main file had eval('$navbar = "' . fetch_template('navbar') . '";'); and I tried to use the new code, but it didn't work. Mar 17, 2011 at 12:31
  • @Jon: Ok. I would need to see the PHP source code to be able to help further.
    – ARandomOWl
    Mar 18, 2011 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.