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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Accesing a variable in a default language

Started by Johanan Marín 1 year ago · 8 replies · 187 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 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 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.

1 year ago

I don't understand either. Si quieres, puedes intentarlo en castellano y dejar que ChatGPT te lo traduzca. En lugar de decir que necesitas una variable, explícanos mejor lo que quieres conseguir y quizás podamos ayudarte más. 🙂

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 10 hours ago
General · by pamtbaau, 15 hours ago
1 51 15 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago