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

Create a variable for my addOn

Discussion in 'XenForo Development Discussions' started by LordsKing, Jul 3, 2014.

  1. LordsKing

    LordsKing Active Member

    Hello :D

    I would to create a variable for my addOn !
    But I don't know how to do it :(

    My variable will be : $user.elite_level

    And the function elite_level will be the number of trophy points !

    I'll explain :

    If the user has 210 trophy points, and the elite level 4 is at 200 trophy points, In the user member card it will be show : Elite : 4

    So the code for show this in the member card is :
    HTML:
    <dt>Elite:</dt> <dd>{$user.elite_level}</dd>
    
    If you don't understand please report it in comment :)

    Bye
     
  2. tyteen4a03

    tyteen4a03 Well-Known Member

    You'll have to either override the Models that make the $user available in the template, or the User Model to have this variable always available when fetching user details.
     
  3. LordsKing

    LordsKing Active Member

    Hi,

    Okay, but how to do it ?
     
  4. Milano

    Milano Well-Known Member

  5. LordsKing

    LordsKing Active Member

    Sorry,

    But I don't understand.
    Why Helpers are here ?
     
  6. Milano

    Milano Well-Known Member

    Helpers can be used in templates for example: {xen:helper richUsername, $user} will render a username with color. So you can create your own helper by following that tutorial then you can use it like that: {xen:helper elitelevel, $user.trophy_points} in your template
     
  7. LordsKing

    LordsKing Active Member

    Oh....
    Thanks :D

    But how can I add the 'elitelevel' in the helper ? :)
     
  8. Milano

    Milano Well-Known Member

    Follow this tutorial http://xenforo.com/community/resources/how-to-create-your-own-helpers.332/ and change this part
    PHP:
    XenForo_Template_Helper_Core::$helperCallbacks += array(
                
    'echo' => array('SimpleHelper_Helpers''helperEcho')
            );
    to
    PHP:
    XenForo_Template_Helper_Core::$helperCallbacks += array(
                
    'elitelevel' => array('your class''your function')
            );
     
  9. LordsKing

    LordsKing Active Member

    Ok, so :

    Is is that ?

    Code:
    XenForo_Template_Helper_Core::$helperCallbacks += array(
    'elitelevel' => array('SimpleHelper_Helpers', 'helperEcho')
    );
    
     
  10. Milano

    Milano Well-Known Member

    you can change this too
    PHP:
    array('SimpleHelper_Helpers''helperEcho')
    It an example, you can change it to whatever you want, but take a note on directory structer
     
  11. LordsKing

    LordsKing Active Member

    Ok, thanks you

    But that will not show the level elite according to user trophy points
     
  12. Milano

    Milano Well-Known Member

    You need to write your own code for that, in the tutorial he create a file Helpers.php in SimpleHelper folder an put in that
    PHP:
    <?php
    //Our class helper (we can put any helpers in here we want)
    class SimpleHelper_Helpers
    {
        public static function 
    helperEcho ($string)
        {
            
    //We only return the argument, dont do nothing.
            
    return $string;
        }
    }
    ?>
    You have to do the same

    PHP:
    <?php
    //Our class helper (we can put any helpers in here we want)
    class yourclass
    {
        public static function 
    yourfunction($points)
        {
            
    //$points = current user trophy points;
            // Write your code
            //$level = ......
            
    return $level;
        }
    }
    ?>
     
  13. LordsKing

    LordsKing Active Member

    Erf...
    It's too difficult for me :/

    I need to assign $level variable at each elite level ?
     
  14. Milano

    Milano Well-Known Member

    It just a simple math, let's say 200 points = lvl 4 that mean 50 points per level and your code is
    PHP:
    $level floor($points 50);
    return 
    $level;
     
    Last edited: Jul 13, 2014
  15. LordsKing

    LordsKing Active Member

    Ok and it show Elite: 4 in the memeber card ?

    And if it's not 50 points per level ?
    Elite 3 : 180
    Elite 4 :200 ?
     
  16. Milano

    Milano Well-Known Member

    Make your own algorithm
     
  17. LordsKing

    LordsKing Active Member

    Ok thanks, and for show the level in the member card ?
     
  18. Milano

    Milano Well-Known Member

    Follow the tutorial then add to your member_card template
    Code:
    <dt>Elite:</dt> <dd>{xen:helper elitelevel, $user.trophy_points}</dd>
     
  19. LordsKing

    LordsKing Active Member

    Thanks you ^^
     

Share This Page