XF 1.5 Dump all template variables to javascript console

infis

Well-known member
Create class for callback. For example library/InfisJSC/Helper.php:
PHP:
<?php

class InfisJSC_Helper
{
    static function convertUTF8 (&$item, $key)
    {
        if (gettype($item) == 'string')
        {
            $item = iconv('UTF-8', 'UTF-8//IGNORE', $item);
        }
    }

    /**
     * Callback function for output dump to javascript console
     *
     * Example:
     * <xen:callback class="InfisJSC_Helper" method="getLog" params="{$__params}">$__params</xen:callback>
     *
     * @param $content
     * @param $params
     * @param $template
     * @return string
     */
    public static function getLog ($content, $params, XenForo_Template_Abstract $template)
    {
        array_walk_recursive($params, array(__CLASS__, 'convertUTF8'));

        $result = XenForo_ViewRenderer_Json::jsonEncodeForOutput($params);

        if (!empty($content))
        {
            $variableName = '\'' . $content . ':\', ';
        } else {
            $variableName = '';
        }

        $templateName = $template->getTemplateName();

        return '
            <script>
                var jObject = ' . $result . ';
                console.log(\'' . $templateName . '\');
                console.log(' . $variableName . 'jObject);
            </script>
        ';
    }
}

Now we can insert in template this call:
Code:
<xen:callback class="InfisJSC_Helper" method="getLog" params="{$__params}">$__params</xen:callback>

In the browser console, we'll see the name of the template, the name of the variable and its dump.
 
Back
Top Bottom