vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Replace Conditionals with Variables? (https://vborg.vbsupport.ru/showthread.php?t=285582)

grey_goose 07-15-2012 03:26 PM

Replace Conditionals with Variables?
 
I've got a ton of conditionals in my postbit, and am wondering if there's a more elegant way to do what I want.

The following swaps backgrounds for usergroups:

HTML Code:

<vb:if condition="is_member_of($post, 11)"><div class="userinfoctl">
<vb:else />
<vb:if condition="is_member_of($post, 36)"><div class="userinfogse">
<vb:else />
<vb:if condition="is_member_of($post, 46)"><div class="userinfohtv">
<vb:else />
<vb:if condition="is_member_of($post, 12)"><div class="userinfomta">
<vb:else />
<vb:if condition="is_member_of($post, 47)"><div class="userinfoptc">
<vb:else />
<vb:if condition="is_member_of($post, 14)"><div class="userinfowod">
<vb:else />
<vb:if condition="is_member_of($post, 13)"><div class="userinfo2">
<vb:else />
<vb:if condition="is_member_of($post, 10)"><div class="userinfowtf">
<vb:else />
<div class="userinfo">
</vb:if>
</vb:if>
</vb:if>
</vb:if>
</vb:if>
</vb:if>
</vb:if>
</vb:if>

Can I get rid of the majority of this with something like:
HTML Code:

<div class="{vb:raw is_member_of}background">
...and then just rename my classes "46background", "12background", etc ?

soniceffect 07-15-2012 08:23 PM

you wouldnt be able to do it that way, as a user could be a member of more than 1 usergroup. is_member_of is a function which checks through the usergroups a user has assigned and returns true or false. Is it only the primary usergroup that you are interested in?

grey_goose 07-15-2012 10:46 PM

Right, it was just an example to demonstrate because I wasn't sure if there was a variable I could use.

Yes, I could probably manage with just using the primary usergroup.

soniceffect 07-16-2012 11:33 AM

If thats the case then could probably do a hook which retrieves primary usergroup from the user and makes it available to the css template and use it as you have stated above. If your not sure on how to do this yourself gimme a shout and will throw one together for you. Unless of course primary group is already available, not sure I would have to look.

Sarteck 07-17-2012 05:45 PM

Create a function, get_member_background($userinfo). Have it return the color you want. Define it in a plugin on global_complete or something.

Do you want to be able to call this function in a template? If so, add another plugin on the template_safe_functions hook, and then add your function name into the allowed functions for conditionals, like so:
PHP Code:

$safe_functions[] = 'get_member_background'

Now, in your Template, you could go something like,


HTML Code:

<vb:if condition="$post['background'] = get_member_background($post)"><div class="{vb:raw post.background}">
<vb:else />
<div class="userinfo">
</vb:if>


soniceffect 07-18-2012 08:09 AM

Quote:

Originally Posted by Sarteck (Post 2349034)
Create a function, get_member_background($userinfo). Have it return the color you want. Define it in a plugin on global_complete or something.

Do you want to be able to call this function in a template? If so, add another plugin on the template_safe_functions hook, and then add your function name into the allowed functions for conditionals, like so:
PHP Code:

$safe_functions[] = 'get_member_background'

Now, in your Template, you could go something like,


HTML Code:

<vb:if condition="$post['background'] = get_member_background($post)"><div class="{vb:raw post.background}">
<vb:else />
<div class="userinfo">
</vb:if>


Oh thats a handy thing to know. :)

Sarteck 07-18-2012 08:14 AM

Yeah, I was wanting to use array_rand() in one of my own modifications a long time ago, to choose a random image from a set of images to display. Found that handy info in the comments on the manual, else I'd never have known. :3

cellarius 07-18-2012 08:45 AM

Apart from other solutions - there is elseif in vB4 conditionals.

<vb:if condition="condition">do smth. if exactly this condition is met.
<vb:elseif condition="condition" />do smth. if exactly this condition is met.
<vb:elseif condition="condition" />do smth. if exactly this condition is met.
<vb:else />do smth. if none of the prior conditions is met.
</vb:if>

grey_goose 07-19-2012 09:16 PM

Awesome, thanks all :)

On a completely unrelated note...

I have two mods that are in {vb:raw template_hook.showthread_above_posts}, how would I change the order they're displayed in?

Mod A
Mod B

to

Mod B
Mod A

Sarteck 07-19-2012 10:50 PM

You can edit the plugin execution order. Most plugins have a default execution order of '5'. Set Mod A to have an execution order of '4'. (That's the simplest way to do it, anyhow.)

grey_goose 07-20-2012 12:18 AM

I *thought* that's what it was. I was just doing it on the wrong hook <facepalm>

Thanks :)


All times are GMT. The time now is 04:54 PM.

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.01239 seconds
  • Memory Usage 1,748KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_html_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (11)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete