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

AndyB

Well-known member
I know I can use this in a template:

{$visitor.username}

but how would I do the same in a phrase?
 
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?
 
You create a phrase with this content
Code:
{x} is better than {y}
Everything inside curly braces act as a place holder.

You use it this way in your template
Code:
{xen:phrase your_phrase_name, 'x={$visitor.username}', 'y={$user.username}'}

Output would be
Code:
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
 
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}.

pic001.webp
 
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)
 
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.
 
Template:

{xen:phrase there_no_messages_on_xs_profile_yet, 'name={$user.username}'}

Phrase:
upload_2015-8-9_0-15-37-png.113684
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.
 
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:
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.
Code:
{xen:phrase sidebardonations_message, 'name={$visitor.username}', 'boardTitle={$xenOptions.boardTitle}'}
Example images:

Template
pic001.webp

Phrase called sidebardonations_message
pic002.webp
 
I'm unclear about this, wouldn't $user.username and $visitor.username give the same value?
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).
 
Top Bottom