htmlHelper::_tagBuilder PHP Method

_tagBuilder() protected method

JS and CSS files are never included more than once even if requested twice. If DEBUG mode is enabled than the second request will be added to the debug log as a duplicate. The jsSingleton and cssSingleton methods operate the same as the js & css methods except that they will silently skip duplicate requests instead of logging them. jsInlineSingleton and cssInlineSingleton makes sure a JavaScript or CSS snippet will only be output once, even if echoed out multiple times and this method will attempt to place the JS code into the head section, if has already been echoed out then it will return the JS code inline the same as jsInline. Eg.: $helloJs = "function helloWorld() {alert('Hello World');}"; echo $html->jsInlineSingleton($helloJs); Adding an optional extra argument to jsInlineSingleton/cssInlineSingleton will return the inline code bare (plus a trailing linebreak) if it cannot place it into the head section, this is used for joint JS/CSS statements: echo $html->jsInline($html->jsInlineSingleton($helloJs, true) . 'helloWorld();');
protected _tagBuilder ( string $tagType, array $args = [] ) : string
$tagType string
$args array
return string
    protected function _tagBuilder($tagType, $args = array())
    {
        $arg = current($args);
        if (empty($arg) || $arg === '') {
            $errorMsg = 'Missing argument for ' . __CLASS__ . '::' . $tagType . '()';
            trigger_error($errorMsg, E_USER_WARNING);
        }
        if (is_array($arg)) {
            foreach ($arg as $thisArg) {
                $return[] = $this->_tagBuilder($tagType, array($thisArg));
            }
            $return = implode(PHP_EOL, $return);
        } else {
            switch ($tagType) {
                case 'js':
                case 'jsSingleton':
                case 'css':
                    //Optional extra argument to define CSS media type
                //Optional extra argument to define CSS media type
                case 'cssSingleton':
                case 'jqueryTheme':
                    if ($tagType == 'jqueryTheme') {
                        $arg = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/' . str_replace(' ', '-', strtolower($arg)) . '/jquery-ui.css';
                        $tagType = 'css';
                    }
                    if (!isset($this->_includedFiles[$tagType][$arg])) {
                        if ($tagType == 'css' || $tagType == 'cssSingleton') {
                            $return = '<link rel="stylesheet" type="text/css" href="' . $arg . '"' . ' media="' . (isset($args[1]) ? $args[1] : 'all') . '" />';
                        } else {
                            $return = '<script type="text/javascript" src="' . $arg . '"></script>';
                        }
                        $this->_includedFiles[$tagType][$arg] = true;
                    } else {
                        $return = null;
                        if (DEBUG_MODE && ($tagType == 'js' || $tagType == 'css')) {
                            debug::log($arg . $tagType . ' file has already been included', 'warn');
                        }
                    }
                    break;
                case 'cssInline':
                    //Optional extra argument to define CSS media type
                    $return = '<style type="text/css" media="' . (isset($args[1]) ? $args[1] : 'all') . '">' . PHP_EOL . '/*<![CDATA[*/' . PHP_EOL . '<!--' . PHP_EOL . $arg . PHP_EOL . '//-->' . PHP_EOL . '/*]]>*/' . PHP_EOL . '</style>';
                    break;
                case 'jsInline':
                    $return = '<script type="text/javascript">' . PHP_EOL . '//<![CDATA[' . PHP_EOL . '<!--' . PHP_EOL . $arg . PHP_EOL . '//-->' . PHP_EOL . '//]]>' . PHP_EOL . '</script>';
                    break;
                case 'jsInlineSingleton':
                    //Optional extra argument to supress adding of inline JS/CSS wrapper
                //Optional extra argument to supress adding of inline JS/CSS wrapper
                case 'cssInlineSingleton':
                    $tagTypeBase = substr($tagType, 0, -15);
                    $return = null;
                    $md5 = md5($arg);
                    if (!isset($this->{'_' . $tagTypeBase . 'Singleton'}[$md5])) {
                        $this->{'_' . $tagTypeBase . 'Singleton'}[$md5] = true;
                        if (!$this->_bodyOpen) {
                            $this->vorkHead[$tagTypeBase . 'Inline'][] = $arg;
                        } else {
                            $return = !isset($args[1]) || !$args[1] ? $this->{$tagTypeBase . 'Inline'}($arg) : $arg . PHP_EOL;
                        }
                    }
                    break;
                case 'div':
                case 'li':
                case 'p':
                case 'h1':
                case 'h2':
                case 'h3':
                case 'h4':
                    $return = '<' . $tagType . '>' . $arg . '</' . $tagType . '>';
                    break;
                default:
                    $errorMsg = 'TagType ' . $tagType . ' not valid in ' . __CLASS__ . '::' . __METHOD__;
                    throw new Exception($errorMsg);
                    break;
            }
        }
        return $return;
    }