• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

how does tag work in XenForo?

#1
I am trying to figure out how tag works in XenForo? eg. <xen:title />. I see all the tag source files are here: library\XenForo\Template\Compiler\Tag\. I take a look at the simple one. Title.php. Below is the code:
Code:
<?php
 
/**
* Class to handle compiling template tag calls for "title".
*
* @package XenForo_Template
*/
class XenForo_Template_Compiler_Tag_Title implements XenForo_Template_Compiler_Tag_Interface
{
    /**
    * Compile the specified tag and return PHP code to handle it.
    *
    * @param XenForo_Template_Compiler The invoking compiler
    * @param string                Name of the tag called
    * @param array                  Attributes for the tag (may be empty)
    * @param array                  Nodes (tags/curlies/text) within this tag (may be empty)
    * @param array                  Compilation options
    *
    * @return string
    */
    public function compile(XenForo_Template_Compiler $compiler, $tag, array $attributes, array $children, array $options)
    {
        if (empty($options['allowRawStatements']))
        {
            throw $compiler->getNewCompilerException(new XenForo_Phrase('x_tags_only_used_where_full_statements_allowed', array('tag' => 'title')));
        }
 
        $var = '__extraData[\'title\']';
        $childOutput = $compiler->compileIntoVariable($children, $var, $options, false);
 
        return $compiler->getNewRawStatement($childOutput);
    }
}
Although there is comment, still had a hard time to understand it.
Take this as an example:
Code:
<xen:title>Page Title</xen:title>
Questions:
1. I think here, $tag=title, $children=Page Title, but what does these variable mean: $compiler?$attributes?options?
2. what does this line mean?
Code:
$var = '__extraData[\'title\']';
 

Uniphix

Active member
#2
$compiler is the actual compiler that compiles the <xen:title> tag into $var = '__extraData[\'title\']';

$attributes are attributes within <xen:title> for example <xen:title mycustomattribute="58">

You can access those attributes in such cases such as this:

PHP:
// user array
        if (!empty($attributes['user']))
        {
            $user = $compiler->compileVarRef($attributes['user'], $options);
        }
        else
        {
            throw $compiler->getNewCompilerException(new XenForo_Phrase('missing_x_attribute_for_tag_y', array(
                'attribute' => 'user',
                'tag' => 'avatar'
            )));
        }
This allows you to ensure that "user" attribute in <xen:avatar user="$user" /> is assigned...

To look at more advance look at XenForo_Template_Compiler_Tag_Avatar in library/XenForo/Template/Compiler/Tag/Avatar.php