Automatically creating a template...

Mr. Goodie2Shoes

Well-known member
How does an add-on automatically creates a template upon adding a content (for example)...
XenForo's Page Node does it, when you create a new page, XenForo automatically creates a template associating the page...
 
PHP:
class Ragtek_DeveloperTools_Generator_Template{
 
    /**                         
     * 
     * @static
     * @param array $addOn
     * @param $title
     * @param $template
     * @return mixed
     * @throws XenForo_Exception
     */
    public static function generatePublicTemplate(array $addOn, $title, $template){
        if (self::checkPublicTemplateExists($title)) {
            throw new XenForo_Exception(sprintf('public template %s exists already', $title));
        }
 
        $propertyModel = self::_getStylePropertyModel();
 
        $properties = $propertyModel->keyPropertiesByName(
            $propertyModel->getEffectiveStylePropertiesInStyle(-1)
        );
 
        $propertyChanges = $propertyModel->translateEditorPropertiesToArray(
            $template, $template, $properties
        );
        /** @var $writer XenForo_DataWriter_Template */
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Template');
        $writer->bulkSet(array(
            'title' => $title,
            'template' => $template,
            'addon_id' => $addOn['addon_id'],
            'style_id' => 0
        ));
 
        try {
            $writer->save();
        } catch (Exception $ex) {
            throw new XenForo_Exception("Exception creating template $title: " . $ex->getMessage() . '<br/><pre>' . htmlentities($template) . '</pre>');
        }
 
        $propertyModel->saveStylePropertiesInStyleFromTemplate(-1, $propertyChanges, $properties);
 
        return $title;
    }
 
Top Bottom