Best way to implement simple abstraction in templates?

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:

HTML:
<xen:if condition="{showAds}">
  <!-- ad code here -->
</xen:if>

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?
 
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.
 
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.
 
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.
 
I'm sorry Andy, but that seems like a pretty convoluted way of doing things.

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.
 
Code:
<xen:if is="{xen:helper ismemberof, $visitor, 1,2,3}">
    Show to $visitor if they are in usergroup 1, 2 or 3
</xen:if>

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?
 
Code:
<xen:if is="{xen:helper ismemberof, $visitor, 1,2,3}">
    Show to $visitor if they are in usergroup 1, 2 or 3
</xen:if>

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?

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.
 
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.
 
Top Bottom