How can I use a raw variable in an if statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andy
    Senior Member
    • Jan 2002
    • 5886
    • 4.1.x

    [Forum] How can I use a raw variable in an if statement?

    I preregistered a variable and I would like to use it in an if statement in a template. For example:

    Code:
    <vb:if condition="{vb:raw $my_preregistered_variable}">
    
    content
    
    </vb:if>
    What is the correct way of doing this?
  • Mark.B
    vBulletin Support
    • Feb 2004
    • 24287
    • 6.0.X

    #2
    Code:
    <vb:if condition="$my_preregistered_variable">
    
    content
    
    </vb:if>
    MARK.B
    vBulletin Support
    ------------
    My Unofficial vBulletin 6.0.0 Demo: https://www.talknewsuk.com
    My Unofficial vBulletin Cloud Demo: https://www.adminammo.com

    Comment

    • reefland
      Senior Member
      • Sep 2000
      • 1131

      #3
      If it's properly registered why not just output the contents in the template? I guess my question is, why the conditional?
      sigpic
      Nation of Blue - Kentucky Wildcats Sports


      Some CMS Goodness: Add Avatar to Article

      Comment

      • Andy
        Senior Member
        • Jan 2002
        • 5886
        • 4.1.x

        #4
        Originally posted by Mark.B
        Code:
        <vb:if condition="$my_preregistered_variable">
        
        content
        
        </vb:if>
        Hi Mark,

        That doesn't work. Preregistered variable have to be in the form of: {vb:raw $my_preregistered_variable}
        Last edited by Andy; Fri 10 Feb '12, 3:40pm.

        Comment

        • Andy
          Senior Member
          • Jan 2002
          • 5886
          • 4.1.x

          #5
          Originally posted by reefland
          If it's properly registered why not just output the contents in the template? I guess my question is, why the conditional?
          When a member has is on another members Ignore List, currently threads are show when viewing the forums. I would like to put a conditional around the threadbit template so that it's not displayed if the thread is started by a member that is on another members Ignore List.
          Last edited by Andy; Fri 10 Feb '12, 3:51pm.

          Comment

          • reefland
            Senior Member
            • Sep 2000
            • 1131

            #6
            {vb:raw $variable} is correct. If you use it in a conditional, then it needs to meet some condition even if it is != "".
            sigpic
            Nation of Blue - Kentucky Wildcats Sports


            Some CMS Goodness: Add Avatar to Article

            Comment

            • kh99
              Senior Member
              • Aug 2009
              • 533

              #7
              Mark.B is correct, if you use it in a conditional it would be $my_preregistered_variable. You can use it without and comparison operator because it will be converted to a boolean based on a set of rules (see the php docs, http://us2.php.net/manual/en/languag...oolean.casting).

              To insert the value other places in the template it would be {vb:raw my_preregistered_variable} (no $).

              Comment

              • reefland
                Senior Member
                • Sep 2000
                • 1131

                #8
                You are right, it is {vb:raw variable}.
                sigpic
                Nation of Blue - Kentucky Wildcats Sports


                Some CMS Goodness: Add Avatar to Article

                Comment

                • Andy
                  Senior Member
                  • Jan 2002
                  • 5886
                  • 4.1.x

                  #9
                  The preregistered variable in my case is equal to the following:

                  $thread['postuserid'] <> '7460'

                  This works just fine to exclude threadid 7460 when viewing the forum it is in.

                  Code:
                  <vb:if condition="$thread['postuserid'] <> '7460'">
                  
                  threadbit template
                  
                  </vb:if>
                  However none of the following work.

                  Code:
                  <vb:if condition="{vb:raw $my_preregistered_variable}">
                  
                  threadbit template
                  
                  </vb:if>
                  Code:
                  <vb:if condition="$my_preregistered_variable">
                  
                  threadbit template
                  
                  </vb:if>

                  Comment

                  • Andy
                    Senior Member
                    • Jan 2002
                    • 5886
                    • 4.1.x

                    #10
                    Originally posted by klh

                    To insert the value other places in the template it would be {vb:raw my_preregistered_variable} (no $).
                    Hi KLH,

                    If I try it without the $ sign, I get the following error:

                    Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3459

                    Comment

                    • kh99
                      Senior Member
                      • Aug 2009
                      • 533

                      #11
                      What does the code look like where you registered it?

                      Comment

                      • Andy
                        Senior Member
                        • Jan 2002
                        • 5886
                        • 4.1.x

                        #12
                        Originally posted by klh
                        What does the code look like where you registered it?
                        I preregister the variable using a plugin which calls a php script.

                        I can insert the preregistered variable in the first line of the threadbit template like this:

                        Code:
                        {vb:raw $my_preregistered_variable}
                        threabit template
                        This will echo the following many times as that threadbit is called many times when displaying a forum.

                        $thread['postuserid'] <> '7460'

                        Comment

                        • kh99
                          Senior Member
                          • Aug 2009
                          • 533

                          #13
                          I don't understand - are you just telling me it works with the '$'? OK, can't argue with the fact that it works for you. I just wondered what your preRegister line looks like because I've always seen it work without the dollar sign.

                          Comment

                          • Andy
                            Senior Member
                            • Jan 2002
                            • 5886
                            • 4.1.x

                            #14
                            My plugin PHP code looks like this

                            Code:
                            ob_start();
                            include('/path/to/my/script.php');
                            $my_preregistered_variable = ob_get_contents();
                            ob_end_clean();
                            
                            vB_Template::preRegister('threadbit',array('my_preregistered_variable' => $my_preregistered_variable));

                            Comment

                            • Andy
                              Senior Member
                              • Jan 2002
                              • 5886
                              • 4.1.x

                              #15
                              It appears there's no way to use a preregistered variable in an if statement.
                              Last edited by Andy; Fri 10 Feb '12, 4:55pm.

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...