1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating Style Templates in PHP code?

Discussion in 'XenForo Development Discussions' started by Jaxel, Nov 2, 2010.

  1. Jaxel

    Jaxel Well-Known Member

    I'm building a portal system, with a modular sidebar... and so far its working great:

    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?
  2. Jaxel

    Jaxel Well-Known Member

    Okay... looking through the code... I think I could theoretically do this:

    $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');
    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:


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

    $dw = XenForo_DataWriter::create('XenForo_DataWriter_Template');
    What about these functions?


    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?
  3. Shadab

    Shadab Well-Known Member

    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
  4. Jaxel

    Jaxel Well-Known Member

    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.
  5. Shadab

    Shadab Well-Known Member

    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:
    'title'    => $template_title,
    'template' => $template_content,
    'addon_id' => 'EWRporta',
    'style_id' => '0'
    instead of,
    dmnkhhn likes this.

Share This Page