Jump to content

Adding my own variable to every page


Meddysong

Recommended Posts

I have a multilingual site and one of the tricks I do to pull in language-specific blocks and images is to name them with the first two letters of the locale appended. For example, "my-image-en.png" and "my-image-eo.png". Then what I do is have a variable which will generate "en" or "eo" depending on the locale in use:

{{$lingvo = substr(\IPS\Member::loggedIn()->language()->short, 0, 2);}}

All i have to do after that is place

{block="course-attendance-{$lingvo}"}

or

<img src='{setting="base_url"}uploads/assets/my-image-{$lingvo}.png' >

in my templates and WHAM!, language-specific content.

My challenge, having somehow managed to plug this all together, is that I'm a total novice. What I'm having to do is create $lingvo every time I want to do this trick. What I'd like to do instead is have $lingvo already existing within the system, kind of like a global variable, except that it's evaluated whenever the language changes or, presumably easier, whenever the page loads.

I haven't got a clue where to do this. Part of me is saying "stick it in the globalTemplate". Is there a better approach for me to take?

Link to comment
Share on other sites

  • 2 weeks later...

Answering my own question, since Marcher explained it to me:

Adding the variable to globalTemplate is the solution but it needs to have global scope, otherwise it won't be picked up when used in other templates. To do this, the code in globalTemplate should be adjusted to

{{\IPS\Settings::i()->lingvo = substr(\IPS\Member::loggedIn()->language()->short, 0, 2);}}

Then if you want to call, say, a language-specific image in the footer (which is a separate template), it works perfectly.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...