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

Unmaintained How to create your own helpers 1.1

I'll teach you how to create your own helpers to use in templates.

  1. Fuhrmann

    Fuhrmann Well-Known Member

    Fuhrmann submitted a new resource:

    How to create your own helpers (version 1.0) - I'll teach you how to create your own helpers to use in templates.

    Read more about this resource...
     
    Yorick and Russ like this.
  2. Chris D

    Chris D XenForo Developer Staff Member

    Fuhrmann, that is a great guide and has helped me do something I wanted to do.

    However, what if, instead of adding a new helperCallback, I wanted to overwrite an existing one?

    Is that even possible?
     
    Fuhrmann likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Fuhrmann likes this.
  4. Fuhrmann

    Fuhrmann Well-Known Member

    Good to know!
     
  5. x7iBiT

    x7iBiT Member

    What's the difference between helper and hook?
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    Hooks are used, usually by add on creators, to inject new code or replace existing code into templates.

    Helpers are usually more specific and serve a specific task. They don't necessarily return something you can see, either. For example there's a helper that can tell you if a member is in a specified user group. That just returns either true or false to use in conditionals.

    Other times helpers are used to render blocks of information. I guess as a time saver. If you imagine the HTML code required to render someone's custom avatar. You need the div container, you need the URL to their current avatar, you need their profile URL etc. Well all that can be produced with a single line of code such as {xen:helper avatar, $user}.

    Helpers aren't always used by add on developers either. Obviously they can be created by add on developers but they can be used by anyone. If you want to add some code visible to only your members in a certain group or display an avatar in a new location then you can do so using any of the predefined helpers.

    I hope that explains it ok for you.
     
    DeltaHF, Fuhrmann and x7iBiT like this.
  7. Liam W

    Liam W Well-Known Member

    Can a helper return an array that can be used in a xen:foreach tag?
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    I believe you would get an array to string conversion helper but I haven't tested it.

    Best thing to do is edit one of the core helpers temporarily and have it output a php array. Then use the helper in a template and verify.

    It's worth noting that you could use a combination of xen:set and xen:callback tags to set the array and then use the set var to loop through the array.
     
  9. Liam W

    Liam W Well-Known Member

    Haha, as luck would have it I just opened the discussion helper and whadda you know, it returns an array!

    I'll have to check the template to see how it's used, but it seems it is possible ;)
     
  10. Chris D

    Chris D XenForo Developer Staff Member

    That's good!
     
    Liam W likes this.
  11. Fuhrmann

    Fuhrmann Well-Known Member

    Razasharp and Pereira like this.
  12. DeltaHF

    DeltaHF Well-Known Member

    Another great tutorial, thanks!

    "echo" in the $helperCallbacks array is just the name of your particular custom helper, right? One thing you might want to mention - it seems this name cannot contain a capital letter. I nearly went crazy trying to figure out why my custom helper didn't work, and that was it - I had named it after the method which did its calculation, which contained a capital letter in camel case.

    Should a custom helper always return a string?
     
    Last edited: Mar 27, 2014
    Sean Kendle, rugk and Fuhrmann like this.
  13. Fuhrmann

    Fuhrmann Well-Known Member

    Hey thanks!

    And yes, 'echo' is the name of the helper.

    And about always returning a string...not always. You can return true or false too. But I guess most of the times you will be returning a string.
     
    DeltaHF likes this.
  14. infis

    infis Well-Known Member

    Can I call my helper with named parameters?

    For example:
    Code:
    public static function username(array $sp_member, $username = '', $rich = false, array $attributes = array())
    I want have analog of username helper with attributes. To call this from templates as:
    Code:
    <h3 class="username">{xen:helper sp_user, $member, '', true, 'class=StatusTooltip'}</h3>
    How I can send to my helper array of parameters?
     
  15. HeadHodge

    HeadHodge Member

    @Fuhrmann,

    Thanks for this great write-up. After reading it though, my first thought is hmmm.... why don't I just use the <xen:callback> tag instead? It's much simpler to do than going through the exercise of creating a helper (IMO).

    Do you think callbacks can do the same thing as helpers?
     
  16. Chris D

    Chris D XenForo Developer Staff Member

    It depends exactly what you want to do, but yeah, callbacks can be useful.

    Worth noting that this tutorial (as valid as it is) way pre-dates the inclusion of the callback tag in XenForo :)
     
  17. HeadHodge

    HeadHodge Member

    Thanks for the reply. (y)
    Yeah that is what I suspected (that it was written before callbacks), that's why I was asking.

    I guess you could use callbacks instead of Hooks too??
     
  18. Chris D

    Chris D XenForo Developer Staff Member

    Yep, you could :)
     
  19. HeadHodge

    HeadHodge Member

    Helpers, Hooks, and everything that starts with an "H". :D
     
  20. Rob

    Rob Well-Known Member

    I coulda sworn this thread was locked earlier :S (sorry for the report)

    Anyway.... @Fuhrmann could the tutorial point out that helper names must be in lower case? I spent hours yesterday cussing you whilst trying to get "myHelper" to work until I went back to the drawing board and then realised they must have to be all lower case. :)

    Is anyone able to go more in depth on this now that callbacks would appear to be a better alternative?
    How would you achieve the example in the op, but with a callback?
     
    Sean Kendle likes this.

Share This Page