Raste
Raste

Reputation: 376

Typo3, how to dynamically change content

Problem

There is the requirement, to swap out the logo, based on a choice, the user makes on first visit. (Logo for internationals and logo for north America)

What I came up with

On fist thought this problem can be solved using a cookie. I implemented some javascript, to check and set the cookie if needed. This works fine so far but using javascript to change to logo isn´t the way to go and now I´m looking for possible solutions to this.

Some thoughts

If following the cookie approach, I´d need a way to swap out the template/partials, based on the cookie. Is there any way to do this using fluid or typoscript perhaps?
Maybe the cookie approach is the wrong way to handle this at all? Maybe this could be done (ab-)using the language selection or something like that?

Any help or ideas are appreciated.
Thanks!

Upvotes: 0

Views: 447

Answers (2)

Raste
Raste

Reputation: 376

Thanks to @thomas-löffler and @jonas-eberle for pointing out the right direction. Thomass solution is meant for Typo3 >= 9. This solution that worked for me on Typo3 ver. 8.7 (also see this solution):

  1. Get the cookie value inside typoscript (setup) like this:
lib.requestedLocation = TEXT
lib.requestedLocation.data = global:_COOKIE|<cookie-name>
  1. After getting the cookie value we can use it with fluid like this:
   <f:variable name="location" value="{f:cObject(typoscriptObjectPath: 'lib.requestedLocation')}" />
   <f:if condition="{location} == 'america'">
      <f:then>
         <img class="logo" src="/path-to-us-logo.svg">
      </f:then>
      <!-- defaulting to international if cookie is not set or somehting else is wrong -->
      <f:else>
         <img class="logo" src="/path-to-international-logo.svg">
      </f:else>
   </f:if>

Upvotes: 0

Thomas L&#246;ffler
Thomas L&#246;ffler

Reputation: 6164

First of all: Please use a question title that describes the problem more precise.

After setting the cookie with your javascript, you can set the logo to render in the TypoScript.

With a condition (https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Conditions/Index.html#request-getcookieparams) you can read the cookie parameter and set the logo depending on the cookie.

[request.getCookieParams()['logo'] == 'us']
lib.logoFile = pathToYourLogos/us.svg
[else]
lib.logoFile = pathToYourLogos/international.svg
[end]

Upvotes: 2

Related Questions