XF 2.2 Using a variable in a phrase

Orit

Active member
Hi
Is there a way to use a variable inside a phrase?
I mean doing something like this:
I have a list of phrases:
classifieds_listing_field_choice.town_A01
classifieds_listing_field_choice.town_A02
classifieds_listing_field_choice.town_A03

I want to show them in the template using the A0* part which is determined as a variable:
{{ phrase('classifieds_listing_field_choice.town_{$listing.custom_fields.town}) }}
but get a syntax error.
Is there a different way to do this?

Thanks!!
 
The syntax error is due to a missing ' before the closing ) .

However, variables in phrases are used like so:
Phrase title: some_x_phrase
Phrase text: Some {{var}} phrase.

But the var needs to be passed.

In your case you can likely do this in the template: {{ phrase('classifieds_listing_field_choice.town_') }} {$listing.custom_fields.town}

Assuming the custom field var is available in the template where the phrase is being shown.
 
The syntax error is due to a missing ' before the closing ) .

However, variables in phrases are used like so:
Phrase title: some_x_phrase
Phrase text: Some {{var}} phrase.

But the var needs to be passed.

In your case you can likely do this in the template: {{ phrase('classifieds_listing_field_choice.town_') }} {$listing.custom_fields.town}

Assuming the custom field var is available in the template where the phrase is being shown.
Thank you @Brogan
I was actually referring to the phrase title.

classifieds_listing_field_choice.town_A01
classifieds_listing_field_choice.town_A02
classifieds_listing_field_choice.town_A03

Each title contains different text (a name of a town) (they are in a drop down menu)
As the titles are similar and defer from each other by using a variable ($A01, $A02, $A03), I am looking for a way to use the variable to display the different phrases (text).

Is it possible to call a phrase by using a variable?

Thanks!!
 
I want to show them in the template using the A0* part which is determined as a variable:
{{ phrase('classifieds_listing_field_choice.town_{$listing.custom_fields.town}) }}
but get a syntax error.
Is there a different way to do this?
You can't do this as phrases are parsed when you save a template and not compiled at run time.

If you want to use a dynamic phrase like this, you need to obtain it in the controller and then pass it over in the viewparam array.
 
UPDATE:
I just found the option to call: getFormattedValue()
but as @benFF just mentioned it is not possible to call it in the template.
I now have a bunch of "<xf:if>'s instead...
 
I wonder if you could create a hidden block with a list of all the possible phrases, so this way they would be in the phrase cache and then use your original method to show the one you actually need. 🤔
 
I wonder if you could create a hidden block with a list of all the possible phrases, so this way they would be in the phrase cache and then use your original method to show the one you actually need. 🤔
I actually went the lazy way and just placed text if the variable matched (instead of using the phrase)
 
To anyone having the same issue-
You can use phrase_dynamic()
as shown here:
 
Top Bottom