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

Best way to implement simple abstraction in templates?

Discussion in 'XenForo Development Discussions' started by DeltaHF, Sep 16, 2013.

  1. DeltaHF

    DeltaHF Well-Known Member

    I want to control which user groups see ads.

    I know the ismemberof helper exists and accepts multiple user group IDs, but I'd like to keep maintenance simple and abstract this logic into a type of function, so I only have to define these user group IDs in one place. Ideally, this would enable me to simply use something like this in templates:

    <xen:if condition="{showAds}">
      <!-- ad code here -->
    What's the best method for me to do this? Should I create my own helper or plugin, or might there be an easier way I'm not familiar with?
  2. Chris D

    Chris D XenForo Developer Staff Member

    I don't know your skillset but ordinarily an add-on developer would either use something like the simple cache or probably even easier, create a custom Admin CP option.

    Today I released an update to my Skimlinks add on.

    This is almost the same thing you're trying to do.

    In its options there is a user group selector.

    Feel free to adapt the code in that to your needs.
  3. Marcus

    Marcus Well-Known Member

    I target ads on my thread_view pages for registerd users from california dialling in from an apple line each monday from 10-12 pm with Google DFP (that's an example, I am not really doing that). You can just add all the other criteria there, you just have to add the template name and usergroup to its custom target criteria and you are done without any coding.

    Edit: If you are doing it to learn how to code, I would definitely take a look at @Chris Deemings latest addon.
  4. AndyB

    AndyB Well-Known Member

  5. Chris D

    Chris D XenForo Developer Staff Member

    I'm sorry Andy, but that seems like a pretty convoluted way of doing things.

    {$visitor.user_id} there's your user_id.

    {xen:helper ismemberof, $visitor, 1, 2, 3} That returns a bool indicating if the $visitor is a member of any of those user groups.
  6. AndyB

    AndyB Well-Known Member

    It depends. If the OP has already learned PHP and MySQL, creating a simple script using my example would take a lot less time than learning how to create an addon.

    At any rate it's good to provide options, the OP will decide which direction he or she will go.
  7. Chris D

    Chris D XenForo Developer Staff Member

    <xen:if is="{xen:helper ismemberof, $visitor, 1,2,3}">
        Show to $visitor if they are in usergroup 1, 2 or 3
    That's not an add-on. That doesn't require any PHP or MySQL.

    The OP is already aware of this solution... but I fail to see why writing your own code and adding extra queries to the page when it isn't necessary is a better option?
    Jeremy likes this.
  8. AndyB

    AndyB Well-Known Member

    I stand corrected, forgot about using template conditionals.

    However if the OP requires banner rotation, using the xen:callback might be an easy solution for that aspect of his needs.
  9. Jeremy

    Jeremy Well-Known Member

    Without testing, you may be able to use the ismemberof function and conditionally set $showAds via <xen:set>. It may need some testing to verify, but it may work.

Share This Page