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

Best practice for find and replace

Discussion in 'XenForo Development Discussions' started by RichardKYA, Mar 6, 2016.

  1. RichardKYA

    RichardKYA Well-Known Member

    When creating an add-on, what is the best practice for find and replace template mods when you have multiple edits to make to the same template?

    For example, if you have to add a class to multiple elements and add a few links all in the same template...

    Is it better to just find and replace the whole template contents?

    Or is it better to do multiple template mods for the same template?

    Or is there another method all together?

    Thank you :)
  2. EQnoble

    EQnoble Well-Known Member

    It is almost case by case...for the example you provided the best thing would be to try to find a pattern unique to all of those elements and write a regular expression to catch them so that a single replacement could catch all of them.

    If no pattern can be derived from the elements which you would like to add classes to within that single template, that would be when you (I) would do plain text replacements or even template swap using xen:comment and an include a custom template (probably only worth doing this if you are massively changing templates and want an easy way to keep track of them).

    Personally I find regex replacements to be way cleaner and the sharp knife to the blunt object of text replacements though sometimes you just don't grab a nailgun when you only need one swing with a hammer.
    RichardKYA likes this.
  3. Brogan

    Brogan XenForo Moderator Staff Member

    In general you should avoid replacing whole templates or even large portions of templates, as that is going to affect other add-ons which apply template modifications to the same template.

    Try to keep the find component as small and specific as possible.

    As mentioned above, regex is useful for finding patterns or referring to entire blocks without having to copy and paste all of the content.
    For example, this:
    Refers to all of this:
    <xen:container var="$head.canonical"><link rel="canonical" href="{xen:link 'canonical:forums'}" /></xen:container>
    <xen:if is="{$xenOptions.boardDescription}"><xen:container var="$head.description">
        <meta name="description" content="{$xenOptions.boardDescription}" /></xen:container></xen:if>
    <xen:container var="$head.openGraph">
        <xen:include template="open_graph_meta">
            <xen:set var="$url">{xen:link 'canonical:forums'}</xen:set>
            <xen:set var="$title">{$xenOptions.boardTitle}</xen:set>
            <xen:set var="$description">{$xenOptions.boardDescription}</xen:set>
            <xen:set var="$ogType">website</xen:set>

    Sometimes though, it's easier just to use the template text, especially if it's referencing a unique piece of code in the template.
    RichardKYA likes this.
  4. RichardKYA

    RichardKYA Well-Known Member

    Thanks! This is all very helpful information. (y)

    Using the example above, will the regex avoid conflicts with other add ons if they are targeting a piece of code that falls in between? Eg: If another add on was targeting...

    <xen:set var="$url">{xen:link 'canonical:forums'}</xen:set>
    I'm off to learn more about regex and clean up my template mods

Share This Page