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

XF 1.2 How to replace all the contents in a template using Template Modification

Discussion in 'XenForo Questions and Support' started by AndyB, Jul 22, 2013.

  1. AndyB

    AndyB Well-Known Member

    I would like to be able to replace all the contents in a template using the Create Template Modification system, is this possible?
  2. Chris D

    Chris D XenForo Developer Staff Member

    You can maybe do it with a regular expression but unless you're releasing an add on you'd might as well just edit the template directly.

    And if you are releasing an add on, don't replace an entire template :p
  3. AndyB

    AndyB Well-Known Member

    Currently I have my modified templates saved as text files. So when I upgrade I just copy/paste the text files over the default templates after reverting all templates. What I would like to be able to do is save the modified templates in the TMS in the Replace input box, that way when I upgrade it would easier.

    Ideally there would be one more radio button that says "Replace entire templete" and it would be positioned where the red arrow is.

  4. Chris D

    Chris D XenForo Developer Staff Member

    This should work:

    Set it to Regular Expression and,

    Find: /(.*?)/is
    Replace: <blank>

    As it happens, this has just helped me find a potential bug...

    It works, though.
  5. Brogan

    Brogan XenForo Moderator Staff Member

    Not really.

    If the template is marked as outdated, unless you use the automatic merge feature you will have to do a diff compare yourself and manually edit your replacement code to update it to the latest version.
  6. Chris D

    Chris D XenForo Developer Staff Member

    And actually it's a bit of a misnomer that the TM system is useful in situations like this. If you're replacing an entire template in this way, it will just be even more difficult to merge any updated templates into it. If a future version of XenForo introduces new functionality or fixes in the templates you entirely replace then you will have to still do some sort of comparison. But in the TM system interface there is no useful diff/merge functionality in this regard.

    As Brogan says, letting the templates go outdated and then doing a per difference comparison and merge into the updated template is going to be much quicker.
  7. AndyB

    AndyB Well-Known Member

    Hi Chris,

    Thank you for taking the time to look into this. I was about to test it and ran into a question. Why isn't there a style version in the TMS? If I want to do a replace for example the ad_above_top_breadcrumb in my style_id 2 I can't do that. It appears only the Default Style style_id 1 can be updated.
  8. Chris D

    Chris D XenForo Developer Staff Member

    The XenForo TM system only operates on the Master Style. It isn't style specific.

    So, once again, editing the templates is going to be much simpler, flexible and upgrade friendly.
  9. AndyB

    AndyB Well-Known Member

    Yes it appears the TM system would require two modifications to do want I want.

    1) Allow replacing all
    2) Allow selecting style id

    I will just create my own Template Modification script to do what I want.

    Thanks, Chris.
  10. Chris D

    Chris D XenForo Developer Staff Member

    What's wrong with just editing the templates?
  11. AndyB

    AndyB Well-Known Member

    Nothing really, that's what I've been doing for the past 11 years. I was hoping to make the saving of modified templates and upgrading the forum software procedure a bit easier.
  12. Chris D

    Chris D XenForo Developer Staff Member

    I think you might need to start from the beginning in terms of the new features in XenForo 1.2.

    You're absolutely right that manual template edits aren't great and actually can make life very difficult.

    However, XenForo 1.2 includes an excellent new system for handling outdated templates. Let's say, for example, that you added a completely new line of code to a template and XenForo also modifies the template and you do an upgrade.

    There will of course be an outdated template. But the difference is, you have the option to go to "Merge Changes".

    In Merge Changes, it will show you all of the differences between your version of the template and the new XenForo version.

    Look at this screenshot:


    The two lines of code highlighted in yellow are where the XenForo version (parent) differs from the custom version (your own edit).

    You get given a very simple choice:

    1) Use the parent version and remove your custom changes.
    2) Keep your custom version.
    3) Keep both versions (this will probably not be a common choice for most things).

    To give you an extra level of control, after you have selected one of the options you can actually edit the text directly (maybe you want to add more, or leave yourself a comment or something).


    Finally, note the area in blue? Quite simply this is a custom edit you've made that has automatically merged itself, e.g. there's no conflicts. All you need to do is double check that you still want the custom change. If you don't, you can actually click to edit it or remove it.

    Finally, click Merge Changes. Job done.

    The benefits are:
    • It's easy to see what's changed in the XenForo template.
    • It's easy to see what you've added to the XenForo template.
    • It's easy to choose what action to take if some of the code conflicts.
    • It's easy to allow it to automatically merge changes.
    • It's easy to make further customisations.
    The point I'm trying to make is: It's now easy to make template edits.

    I urge you and everyone else. Do not be scared of making template edits anymore. There's an incredible amount of change control.

    Even if you do all this and realise you've made a massive mistake and merged a load of stuff you shouldn't, you can just use the template history:


    This is all detailed here:

    Valhalla, DBA, Steve F and 3 others like this.
  13. AndyB

    AndyB Well-Known Member

    When I'm editing a template in the Default Style, there's a check box with the following message:

    "Prevent template modifications from being automatically applied"

    So apparently the TM system does apply the changes to the Default Style.
  14. Jeremy

    Jeremy Well-Known Member

    It applies them per-style, globally without restraints.
  15. AndyB

    AndyB Well-Known Member

    Looks like the Template Modification system doesn't apply changes to any of the templates directly, rather it provides instructions in the template_compiled table to find and replace items. However if you make changes in the template those override the TM system.
  16. Jeremy

    Jeremy Well-Known Member

    The only way a template change should override a template modification is if your change invalidates the search and the template modification can't find what its searching for.
  17. AndyB

    AndyB Well-Known Member

    I use this to replace all content in a template:

    Find: /^.*$/s
    Replace: <xen:comment></xen:comment>
  18. Joe Link

    Joe Link Well-Known Member

    Bear with me here, stumbling through this :)

    If I have multiple sites and I'm looking to standardize template modifications across them, is it best to use the above advice (direct template edits) along with parent/child styles, rather than create template modifications and export them via addons?

    I imagine the answer is 'yes', but I want to make sure I'm not creating more work for myself.
  19. Chris D

    Chris D XenForo Developer Staff Member

    Yes. Though arguably using TMs rather than reproducing the same thing over and over again could be sensible but it's not always a better solution. Ultimately it is better as a developer tool.
  20. Joe Link

    Joe Link Well-Known Member

    Thanks for the quick reply, Chris. Can you please clarify what you mean by "reproducing the same thing over and over again"?

Share This Page