[SOLVED] Converting PHP to Fusion Code

Because I didn’t work with TypoScript or Fusion so far I wonder if it’s possible to bring some PHP code in my Neos template. It’s just a small piece of code for reading a cookie but I could not find yet a way to bring this in my template.

<?php if(!(isset($_COOKIE['legalaccept']) && $_COOKIE['legalaccept'] == 'yes') ): ?>
    <script>
        var legal = true;
    </script>
<?php endif; ?>

Hello Bernhard,

look at the “request”-object that is available in the fusion-context:

# show the cookie __your_cookie_name__
#
debug = Neos.Fusion:Debug {
    value = ${request.parentRequest.cookies.__your_cookie_name__}
}

Something along those lines should do the trick:

# render tag based on cookie values
#
content = Neos.Fusion:Tag {
  @if.hasCookie = ${request.parentRequest.cookies.legalaccept && 
  request.parentRequest.cookies.legalaccept.value == 'yes' }
  tagName = "script" 
  content = 'var legal = true;'
}

# ensure sparate caching for users that accepted 
#
prototype(Neos.Fusion:GlobalCacheIdentifiers) {
  legalaccept = ${(request.parentRequest.cookies.legalaccept.value == 'yes')}
}
3 Likes

Hi Martin,

thank you very much for your detailed answer! This is really helpful for me!

Nice. Just one minor nitpick:
If you use request.httpRequest.* instead of request.parentRequest.* it keeps working even if nested in a plugin etc.

4 Likes

Hey, i have the same problem, but it will not work.

This gives me the map, but it did not give me a div with the content cookie.

prototype(WG.Site:Content.GoogleMaps) < prototype(Neos.Neos:ContentComponent) {
    maps = ${q(node).property('maps')}
    layerImage = ${q(node).property('layerImage')}
    layerText = ${q(node).property('layerText')}


contentMaps = Neos.Fusion:Tag {
  @if.hasCookie = ${request.httpRequest.cookies.customMaps && 
  request.httpRequest.cookies.customMaps.value == 'yes' }
  tagName = "div" 
  content = 'Cookie'
}

    renderer = afx`

        <div @if.hasMaps={props.maps} class="responsive-maps">
            {props.maps}
        </div>
         <div @if.hasMaps={!props.maps && !props.renderFrontend}>
            Bitte den Google Maps Code eingeben!
        </div>

        {props.contentMaps}

    `
}

prototype(Neos.Fusion:GlobalCacheIdentifiers) {
  legalaccept = ${(request.httpRequest.cookies.customMaps.value == 'yes')}
}

Hi @BoxerBuffa

This thread is three years old - and a lothave changed :slight_smile: See my latest response to your original thread

https://discuss.neos.io/t/cookie-based-rendering-in-fusion/5581/5