Creating Style Templates in PHP code?

Jaxel

Well-known member
I'm building a portal system, with a modular sidebar... and so far its working great:
http://xen1.8wayrun.com/portal/

The only real issue I have is that for each module, I have to manually create a template for it. When I release this system publicly, I don't want to have to force administrators to create templates for their modules. I would like to be able to add templates to the master list automatically. I realize I could do this by creating an "add-on" for the module; but then I could imagine the add-on list getting extremely large and cumbersome... when it would only be just 1 real addon, and then 30 modules for that addon.

So my question, is there a way to create (and in the reverse, delete) master templates through code?
 
Okay... looking through the code... I think I could theoretically do this:

Code:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_Template');
$dw->set('title', $template_title);
$dw->set('style_id', '0');
$dw->set('template', $template_content);
$dw->set('addon_id', 'EWRporta');
$dw->preSave();
$dw->save();

However, is there anything else I need to do? I know the system does a lot of caching and I don't think just this code is enough to handle the template. I can see there are a lot of additional functions in the datawriter, stuff for:

_postSave
_recompileTemplate
_writeDevFileOutput
_writeMetaDataDevFileOutput

Would I need to make use of any of these as well? Also, what about handling the delete?

Code:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_Template');
$dw->setExistingData($template_id);
$dw->delete();

What about these functions?

_postDelete
_deleteMappedData
_deleteExistingDevFile

Remember, I am still new to object oriented programming. I know I have to manually initiate preSave, but do I need to manually initiate postSave and postDelete?
 
No, you don't need to call any pre/post-save methods explicitly. XenForo_DataWriter::save() automatically calls the appropriate methods for you.

Take a look at the actionSave() method in XenForo_ControllerAdmin_Template class.
in /library/XenForo/ControllerAdmin/Template.php, line 140-180
 
Hmm... I remember in VB, I had to explicitly call the preSave command... but I didn't have to run the postSave command... it was very odd.

I also see a bulkSet command... thats interesting... never seen that before.
 
I also see a bulkSet command... thats interesting... never seen that before.
That's a shortcut method you can call when you want to set multiple data fields in a single statement. Internally it just calls XenForo_DataWriter::set() on each of the array elements passed. So you can do:
PHP:
$dw->bulkSet(array(
	'title'    => $template_title,
	'template' => $template_content,
	'addon_id' => 'EWRporta',
	'style_id' => '0'
));
instead of,
PHP:
$dw->set('title', $template_title);
$dw->set('style_id', '0');
$dw->set('template', $template_content);
$dw->set('addon_id', 'EWRporta');
 
Top Bottom