public function template($pattern, $dataset = array(), $map = 'id')
    {
        if (count($dataset) == 0) {
            return false;
        }

        try {

            $template = '';
                            
            if (is_string($pattern)) {
                $engine = new FabricationEngine();
                $engine->setOption('doctype', 'html.5');
                $engine->loadHTML($pattern);

                $templateDiv = $engine->getDiv();

                if ($templateDiv) {
                    $template = $templateDiv->item(0);
                }
            }

            if (is_object($pattern)) {
                $template = $pattern;
            }

            // Create an empty container, from the template node details.
            if (is_object($template)) {

                $container = $this->create($template->nodeName, $template->nodeValue);

                foreach ($dataset as $key => $row) {

                    // process the template child nodes.
                    foreach ($template->childNodes as $child) {

                        if ($child->nodeName == '#text') {
                            continue;
                        }

                        if (is_object($child->attributes->getNamedItem($map))) {

                            $mappedName = $child->attributes->getNamedItem($map)->nodeName;
                            $mappedValue = $child->attributes->getNamedItem($map)->nodeValue;

                            $nodeAttributes = array();
                            foreach ($child->attributes as $attribute) {
                                $nodeAttributes[$attribute->nodeName] = $attribute->nodeValue;
                            }

                            if (in_array($mappedValue, array_keys($row))) {

                                // create the mapped node attribute with updated numeric key.
                                $nodeAttributes[$mappedName] = $mappedValue . '_' . ($key + 1);

                                // fabricate the new child nodes.
                                $node = $this->create($child->nodeName, $row[$mappedValue], $nodeAttributes
                                );

                                $container->appendChild($node);
                            }
                        }
                    }
                }
                return $container;
            }

            return false;
        } catch (\Exception $ex) {
            echo $ex->getMessage();
        }
    }