This section contains detailed articles elaborating on some of the common issues phpBB users encounter while using the product. Articles submitted by members of the community are checked for accuracy by the relevant phpBB Team. If you do not find the answer to your question here, we recommend looking through the Support Section as well as using the Site Wide Search.

How to: template_bitfield and BBcodes

Description: This article tells you what the template_bitfield does, what bbcode templates can be replaced and how you can produce your own template_bitfield value.

In Categories:

Link to this article: Select All
[url=https://www.phpbb.com/support/docs/en/3.1/kb/article/how-to-template-bitfield-and-bbcodes/]Knowledge Base - How to: template_bitfield and BBcodes[/url]

In this article I will try to explain to you how the bbcode system in templating works. This article however will only focus on the usage and creation of the template_bitfields and tell something about the various bbcode replacement tags which are used.

What are the bitfields used for?
First let me explain what the template_bitfield is used for, everyone has already seen the following in the template.cfg file of prosilver:

Code: Select all

template_bitfield = lNg=

The value you see here, is an encoded string of switches. But before I can explain to you how this encoded string works and what it means, I will first explain the usage of the bbcode.html file.

bbcode.html
The bbcode.html file is the file in which you can specify the HTML replacements for each bbcode. The bbcode parser however has some default hardcoded HTML strings for a few of these bbcodes. This means that for these bbcodes you are not required to define a HTML replacement string.

Replacement string syntax
The syntax of a bbcode replacement string is as follows:

Code: Select all

<!-- BEGIN replacement -->{HTML_REPLACEMENT}<!-- END replacement -->

the replacement is the name of the bbcode tag or part of the bbcode tag you are replacing. Some BBcodes have multiple replacement strings for a single bbcode, an example for this is the [ b ] bbcode, see example below.

Code: Select all

<!-- BEGIN b_open --><strong><!-- END b_open -->
<!-- BEGIN b_close --></strong><!-- END b_close -->

As you can see the [ b ] has an open and close replacement string.

Overview of the bbcode strings
Here is a short overview of all the bbcode strings you can include in your bbcode.html file. First the ones which are required, the second list contains the one which also have a hardcoded replacement.

Required strings
Lists
ulist_open: Opening tag for the unordered list.
ulist_open_default: Opening tag for unordered list, default.
ulist_close: Closing tag for unordered list.

olist_open: Opening tag for ordered list.
olist_close: Closing tag for ordered list.

listitem: Openin tag for list item.
listitem_close: Close tag for list item.

Quotes
quote_username_open: Quote open tag when a username is quoted.
quote_open: Quote open tag when no username is quoted.
quote_close: Quote closing tag.

Code
code_open: Code open tag.
code_close: Code closing tag.

Attachments
inline_attachment_open: Inline attachment open tag.
inline_attachment_close: Inline attachment close tag.

Oneliners
flash: Flash replacement.
url: url link replacement.

Included strings
email: email replacement.
color: font-color replacement.
size: font-size replacement.
img: image replacement.

b_open: bold open tag.
b_close: bold close tag.

i_open: italic open tag.
i_close: italic close tag.

u_open: underline open tag.
u_close: underline close tag.

How do I create the template_bitfield
Now that we know which bbcodes we can replace, which are required and which are not. We can tell the phpBB system which of the bbcodes we want to replace and which we do not. The easiest way for doing this is letting some php code parse our string of "1" and "0" values for the various bbcodes. I will first explain how to generate your list of "1" and "0" values and then give you a script which will output the template_bitfield for you.

Generate the bitlist
Simply follow the following list. If you want to replace the bbcode, you simply write a "1", if you don't want to replace the bbcode you write a "0".
  • Quote
  • Bold
  • Italic
  • Url
  • Img
  • Size (Font)
  • Color (Font)
  • Underline
  • Code
  • List
  • Email
  • Flash
  • Attachment
So for example the template_bitfield for prosilver. In prosilver we want to have the following replaced: Quote, url, size, Code, List, Flash, Attachment.
Then we would end up with the following bitstring.

Code: Select all

1001010011011


Creating the bitfield
The following script will transform your bitstring into the bbcode bitfield, to use within your template.cfg file.

To use the script, simply replace the string which is assigned to $bit_string

Code: Select all

<?php
/**
*    template_bitfield generator.
*    Based on class bitfield.
*    License: GNU GPL
*/

// insert your string of "1" and "0" values here. For example:
// $bit_string = '1001010011011';
$bit_string = '1001010011011';

// Use stripped phpBB code to generate our bitfield.
$template_bitfield = new bitfield();
for ($i = 0; $i < strlen($bit_string) ; $i++)
{
    if ($bit_string[$i] == '1')
    {
        $template_bitfield->set($i);
    }
}
echo $template_bitfield->get_base64();
// Stripped down class from: functions_content.php
class bitfield
{
    var $data;

    function set($n)
    {
        $byte = $n >> 3;
        $bit = 7 - ($n & 7);

        if (strlen($this->data) >= $byte + 1)
        {
            $this->data[$byte] = $this->data[$byte] | chr(<< $bit);
        }
        else
        
{
            $this->data .= str_repeat("\0", $byte - strlen($this->data));
            $this->data .= chr(<< $bit);
        }
    }

    function get_base64()
    {
        return base64_encode($this->data);
    }
}
?>


The script as given here will produce the lNg= template_bitfield as given within the prosilver template.
Conclusion
This concludes this article regarding template_bitfield within the template.cfg file. I hope you now have a better understanding of this variable. Apart from that it should also give you a better understanding of what the bbcode_bitfield is used for.