Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Accesing a variable in a default language

Started by Johanan Marín 1 year ago · 8 replies · 303 views
1 year ago

Hi all!

I'm looking to use a variable as a global variable for that page in all the languages of my website so I don't have to modify each time. Let me explain myself:

I use a text variable called img_pos to set the background-position of the image. I wish to call the variable in the default language (in my case 'ca') so that in the template I can call it or change it only once.

Is it possible?? GPTs say to use page.translation('ca').header.img_pos but doesn't work...

P.D. by the way another issue I have is that {{ dump() }} doesn't nothing so I use vardump to debug...

Help me pls!!

1 year ago

im really not sure if i understood right but if what you ask is this:
" i want a variable that works only for default language which is ca, how can i do that in template ? "
here is a code to solve that, put these to near top of twig file if you need

TWIG
{% set defaultLanguage = grav.language.getDefault %}
{% set activeLanguage = grav.language.getActive|defined(defaultLanguage) %}

later you can do this for variables that appiles for only spesific languages

TXT
if activeLanguage is ca do this
else do this
endif

that is all, have a nice day :)

1 year ago

No... I didn't explain myself well it seems... let me try again.

I have a variable named img_pos in a template so I can use it calling {{ page.header.img_pos }}. What I need is to call the variable of a specific translation (default language) so if I'm showing the 'es' translation of the same page it calls the 'ca' vesion of the variable. Is it possible?

1 reply
1 year ago

Thank you very mu ch for the effort but no... sorry, but I feel I don't explain myself well yet...

This is the website... https://noemipasquina.com/ca/agenda
As you can see, there is a picture for each event. This picture is background-image loaded by URL and placed differently so the customer can adjust the exact y position with the next code:

TWIG
style="background-image: url('{{ isset(p.header.img) ? p.media[p.header.img].url() : p.media[p.header.image].url() }}'); background-position-y: {{ p.header.img_pos }}%"

Now, I want to be able to use the variable saved in the default language version of the same page (in my case is 'ca') So that I use the same data for all the translations. Can anyone understand me now?

1 reply
1 year ago

@Johanan, I'm afraid I still don't understand what you are trying to achieve, but... If you have any results/feedback to share, please do, so we can close the issue.

Suggested topics

Topic Participants Replies Views Activity
General · by Andy Miller, 2 days ago
0 64 2 days ago
General · by Veehem, 3 days ago
0 107 3 days ago
General · by milkboy, 4 days ago
0 110 4 days ago
General · by ian russell, 2 weeks ago
2 203 1 week ago
General · by pamtbaau, 1 month ago
2 459 2 weeks ago