Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Simplify your Template Conditionals
kall's Avatar
kall
Join Date: Apr 2004
Posts: 2,608

 

New Zealand
Show Printable Version Email this Page Subscription
kall kall is offline 12-05-2004, 10:00 PM

If you want to be able to show/hide things in your templates based on Usergroups, you can either use something like:

Code:
<if condition="$bbuserinfo['usergroupid'] == '9' || $bbuserinfo['usergroupid'] == '10' || $bbuserinfo['usergroupid'] == '11'">
or

Code:
<if condition="in_array($bbuserinfo[usergroupid], array(9,10,11))">
...but if those usergroup ids ever change, or you want to add more groups to the array, it could get time consuming, especially if you have multiple styles.

Wouldn't it be easier to be able to use:

Code:
<if condition="is_paid_member()">
?

Well now you can!

In includes/functions.php, find:
PHP Code:
// ###################### Start build datastore #######################
function build_datastore($title ''$data '')
{
    global 
$DB_site;

    if (
$title != '')
    {
        
$DB_site->query("
            REPLACE INTO " 
TABLE_PREFIX "datastore
                (title, data)
            VALUES
                ('" 
addslashes(trim($title)) . "', '" addslashes(trim($data)) . "')
        "
);
    }

After, add
PHP Code:
function is_paid_member($usergroupid 0// replace is_paid_member with your new function/condition
{
    global 
$bbuserinfo;

    if (
$usergroupid == 0)
    {
        
$usergroupid $bbuserinfo['usergroupid'];
    }
    return 
in_array($usergroupid, array(91011));  // replace with the Usergroup ids you want

In adminfunctions_template.php find:

PHP Code:
// vBulletin-defined functions
            
'can_moderate',          // obvious one
            
'can_moderate_calendar'// another obvious one
            
'exec_switch_bg',        // harmless function that we use sometimes
            
'is_browser',            // function to detect browser and versions
            
'is_member_of',          // function to check if $user is member of $usergroupid 
under, add:

PHP Code:
'is_paid_member',          // function to check if $user is member of paid member groups 

Save and upload includes/functions.php and adminfunctions_template.php and you are done.

You can now wrap
Code:
<if condition="is_paid_member()"></if>
around whatever you want to show to people in Usergroups 9, 10 or 11...and if you need to add usergroup 12 to the list, all you need to do is add it in the function.

Hoorah!
Reply With Quote
  #2  
Old 12-06-2004, 08:56 AM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

ooo nice
Reply With Quote
  #3  
Old 12-06-2004, 09:16 AM
Revan's Avatar
Revan Revan is offline
 
Join Date: Jan 2004
Location: Norway
Posts: 1,671
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I feel confident when I say this won't work right away....I think
I only saw this when looking through vB files that there's some array of safe functions that the template parses, and it ignores everything else... I might be wrong, so dont shoot me!
Reply With Quote
  #4  
Old 12-06-2004, 09:27 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Your thread title doesn't really simplify all template conditionals as it would imply. Something along the lines of 'Easy template conditional to select users from particular usergroups'. Thanks for releasing btw
Reply With Quote
  #5  
Old 12-06-2004, 09:43 AM
Erwin's Avatar
Erwin Erwin is offline
 
Join Date: Jan 2002
Posts: 7,604
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Good idea, but you can simplify it even more by adding it as an Admin CP Options settings variable.

Because if you think changing a template is a pain when you want to add usergroups, at least you can do it anywhere. Changing a PHP file to add usergroups is much harder.

You're better off adding the group in your phpinclude_start template -

e.g.

$specialgroupid = "1,2,3,4,5";

Then using that anywhere in your templates:

<if condition="in_array($bbuserinfo[usergroupid], array($specialgroupid))">

Then just change the numbers in your phpinclude_start template anytime.
Reply With Quote
  #6  
Old 12-06-2004, 07:59 PM
kall's Avatar
kall kall is offline
 
Join Date: Apr 2004
Location: New Zealand
Posts: 2,608
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Revan
I feel confident when I say this won't work right away....I think
I only saw this when looking through vB files that there's some array of safe functions that the template parses, and it ignores everything else... I might be wrong, so dont shoot me!
Oopsie. You're right...I realised this as I was falling asleep last night. adminfunctions_template.php needs an edit too.

Sorted.

@Dean: I know...I'm crap at descriptive titles that don't run the length of 2 pages. 'Add new Usergroup Macros for Template Conditionals' was one I played with. Feel free to edit the title to something a bit better.
Reply With Quote
  #7  
Old 12-06-2004, 09:18 PM
Koutaru's Avatar
Koutaru Koutaru is offline
 
Join Date: Feb 2003
Location: IL
Posts: 589
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Really nice! I was wondering how to do this a while back!
[high]* Koutaru installs [/high]
Reply With Quote
  #8  
Old 01-19-2005, 12:49 PM
neocorteqz's Avatar
neocorteqz neocorteqz is offline
 
Join Date: May 2002
Location: Barefoot Bay Fl
Posts: 473
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

this thread was of some use. Thanks.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:09 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04051 seconds
  • Memory Usage 2,284KB
  • Queries Executed 23 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_code
  • (4)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (7)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete