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

XF 1.5 How can I use a variable like visitor.username in a phrase?

Discussion in 'Styling and Customization Questions' started by AndyB, Aug 9, 2015.

  1. AndyB

    AndyB Well-Known Member

    I know I can use this in a template:


    but how would I do the same in a phrase?
  2. Steve F

    Steve F Well-Known Member

    This may help -

    {xen:phrase there_no_messages_on_xs_profile_yet, 'name={$user.username}'}
    BobbyWibowo likes this.
  3. AndyB

    AndyB Well-Known Member

    Hi Steve,

    Thanks for offering to help.

    Can someone please explain how this code is used.
    Last edited: Aug 9, 2015
  4. Steve F

    Steve F Well-Known Member

  5. AndyB

    AndyB Well-Known Member

    Thanks again, Steve.

    So in post #4 you show how to create the phrase, but where is the code you show in post #2 used? Is it put into the template that calls the phrase?
  6. Steve F

    Steve F Well-Known Member

    Yeah, look in the member_view template for it's use.
  7. AndyB

    AndyB Well-Known Member

    Sorry I still don't understand. What do I put in the template?
  8. batpool52!

    batpool52! Well-Known Member

    You create a phrase with this content
    {x} is better than {y}
    Everything inside curly braces act as a place holder.

    You use it this way in your template
    {xen:phrase your_phrase_name, 'x={$visitor.username}', 'y={$user.username}'}
    Output would be
    AndyB is better than SomeOtherUsernameHere
    AndyB is {x} place holder and SomeOtherUsernameHere is {y} placeholder. AndyB is coming from $visitor.username and SomeOtherUsernameHere is coming from $user.username
    BobbyWibowo likes this.
  9. AndyB

    AndyB Well-Known Member

    Hi batpool52!,

    Thank you for your example.

    Unfortunately I'm still lost. What I need is to use a variable in a phrase, it seems your example is for template only.

    So for example I would like to have a phrase like this which will use a variable {username}.

  10. batpool52!

    batpool52! Well-Known Member

    Can you show the code example where the above phrase is used? That would allow me to understand the question more clearly (English isn't my first language :D)
  11. Steve F

    Steve F Well-Known Member

    I think you're getting confused by the 'name=' part in which case the screenshot I posted has {name} in the Phrase text.

    Have you tried what I posted with your code? {$user.username} is being stored in 'name' ({name}) for use in the phrase. Just like @batpool52! examples shows {x} and {y} placeholders.
  12. Daniel Hood

    Daniel Hood Well-Known Member


    I'm not sure how this is confusing you?

    {xen:phrase there_no_messages_on_xs_profile_yet, 'name={$user.username}'} is the code that goes in the template. (Change $user.username to $visitor.username if you're trying to use the current visitor's info instead). Then go create the phrase "there_no_messages_on_xs_profile_yet" (first parameter in the template call), with the content "there are no message on {name}'s profile yet." I've bolded the parts that match up.

    @Steve F and @batpool52! have done a great job explaining this, I think you're over complicating it, just try doing his exact instructions and after seeing it work, go from there.
    batpool52! and Steve F like this.
  13. AndyB

    AndyB Well-Known Member

    I finally got it, thanks everyone for your help.

    This is what I learned.

    In order to use a variable in a phrase, one must add additional code to the normal phrase code. The example below is a normal phrase code used in a template without any additional code:
    {xen:phrase sidebardonations_message}
    In the template code below I added two variables to the normal phrase code which will be passed to the phrase, {name} and {boardTitle} will both be passed to the phrase.
    {xen:phrase sidebardonations_message, 'name={$visitor.username}', 'boardTitle={$xenOptions.boardTitle}'}
    Example images:


    Phrase called sidebardonations_message
    adwade likes this.
  14. AndyB

    AndyB Well-Known Member

    I'm unclear about this, wouldn't $user.username and $visitor.username give the same value?
  15. BobbyWibowo

    BobbyWibowo Well-Known Member

  16. BobbyWibowo

    BobbyWibowo Well-Known Member

    It depends on where you use it. Take message_user_info template, for example. On that template, $user contains info of the message whose user it belongs to. But $visitor will always contains info of the one currently viewing the template on the live site (literally, the visitor).
    AndyB likes this.

Share This Page