How to use a string as {{ @string }} in a template?

72 views
Skip to first unread message

Jean-Marc Tourreilles

unread,
Jun 16, 2017, 1:41:57 AM6/16/17
to Fat-Free Framework

Hello

I am stuck with this problem: I want to use in a template the value returned by a variable like {{ @myVariable }} as the name of a variable. The idea is to be able to translate a string with a dict file.

Example:

in the file /dict/fr.ini, I have a row like this:

name = nom

then in my template, I need to have something like:

<p>{{ @name }}</p>

in my database, I have a column varDictName defining the name of the variable to find in the file fr.ini, so my query return something like $row[varDictName] (whose value here is "name"), and then, in my repeat loop, I use something like:

<p>{{ @row.varDictName }}</p>

which displays:

<p>name</p>

since {{ @row.varDictName }} can't be used to retrieve automatically the good translation in the dict file. It is logical.

I can't find how I can use the string displayed by {{ @row.varDictName }} as the name of a variable like {{ @name }} and do it display:

<p>nom</p>

I hope that I was clear.

Many thanks for your help.

xfra35

unread,
Jun 16, 2017, 4:24:52 AM6/16/17
to Fat-Free Framework
You'd better define a dictionary PREFIX not to polluate the global variable namespace:
 
$f3->PREFIX='DICT.'

That will also help you in this case, since you'll be able to call the DICT array with the key retrieved from the database:

<p>{{ @DICT[ @row.varDictName ] }}</p>

If you don't want to use a prefix, then you need to prefix the variable name with a $:

<p>{{ $@row.varDictName }}</p>



Jean-Marc Tourreilles

unread,
Jun 16, 2017, 2:37:15 PM6/16/17
to Fat-Free Framework
Thank you very much xfra35, the solution with the PREFIX works perfectly. I didn't really understand why and how  it works, but it works ;-)

The second solution putting just a $ before the variable name doesn't work, and, again,  I don't no why... Nothing is displayed in this case. I tried with @@ also, but no more success. 

Well the first solution works, it's ok for me at this time, I will tried to understand later.

Thank you very; very much !

Jean-Marc Tourreilles

unread,
Jun 16, 2017, 3:09:52 PM6/16/17
to Fat-Free Framework
The solution with the PREFIX=DICT. is that I need to change all my templates to put @DICT before all my existing variables. So if somebody knows how to make the solution with just the $ before the name of the variable, it would be better for me.

Thanks for your help

ikkez

unread,
Jun 17, 2017, 8:39:16 AM6/17/17
to Fat-Free Framework
I think it's better to use a PREFIX, so you do not need to care about possible collisions of translation keys with actual controller data you pass to the templates.

Jean-Marc Tourreilles

unread,
Jun 26, 2017, 1:39:52 PM6/26/17
to f3-fra...@googlegroups.com
Thank you ikkez. I did the changes in all my website, now it works perfectly. And I am clean !
Reply all
Reply to author
Forward
0 new messages