3

I have a problem which may be very simple to solve, but I just can't get my head around it.

I would like to do an alternative landing page if an image has been uploaded to the 'homepage' single entry by the client.

This part is straightforward as I could just test if the image was there and if so enable a different piece of html from the normal index page of the site, this would function as my main entry page then.

The problem I see is once a user clicks the homepage takeover they should be redirected to the normal index page. It seems that this will trigger the same bit of code again and will show the takeover page again though.

I am probably over-thinking this one but is there a way to show the homepage takeover once and then if the user clicks through or selects the homepage from the nave it won't trigger the takeover to be shown again?

Any help on this would be appreciated.

4

There's this nice little plugin "LJ Cookies" that allows you to set and retrieve cookies from your template. So you don't need to write your own plugin for this. Just install the plugin and use code like this on your landing page:

{# Retrieve the cookie's value #}
{% set visited = craft.lj_cookies.get('visited') %}

{# Don't show spezial HTML if user visited this page before #}
{% if visited != 1 %}
    <h1>Hello stranger!</h1>
{% endif %}

{# Set a cookie on the user's first visit #}
{% do craft.lj_cookies.set('visited', '1', '', '/') %}
6
  • I'll have to check out that plugin. Is one of those properties a timeout? Dec 15 '14 at 8:09
  • @Douglas I just edited the code, so that it sets the new cookie after trying to retrieve it.
    – carlcs
    Dec 15 '14 at 8:12
  • Was just thinking you might want to set a timeout so that if they come back in an hour or tomorrow they will see it again. From the plugin docs... {% do craft.lj_cookies.set('myname', 'myvalue', now | date_modify("+1 hour").timestamp ) %} Dec 15 '14 at 8:15
  • @Douglas it's a simple wrapper for the PHP setcookie function. So no, it doesn't allow you set a timeout. That third param is for the cookie duration.
    – carlcs
    Dec 15 '14 at 8:15
  • duration.. that's what I meant to say ;) Dec 15 '14 at 8:17
4

There are a few ways I can think to do this.

  1. Create separate 'index.html' and 'home.html' templates.

    • 'index.html' would have a conditional to display the takeover based on your field value, whereas 'home.html' would not. Both however could make use of the same content using {% include '/home.html' %} within 'index.html'. The nav would then link to '/home' rather than '/'.
  2. Set up a custom route and test for the uri segment.

    • If you want to use the same template you could also set a custom route so that '/home' points to your 'index.html' template, and then test for the uri segment using {% if craft.request.getLastSegment() == 'home' %}... {% endif %}
  3. Pass a variable to the 'index.html' template when linking via nav

    • If you pass a variable to your index page when accessed it through the primary nav (i.e. /index.html?t=1), you can then test for the variable using {% if craft.request.getParam('t') %}... {% endif %}. If the variable exists then don't display the takeover. Not a very pretty URI though.
  4. Write a small plugin to set a cookie.

    • You could also write a small plugin, with a variables file that sets a cookie: (not tested)
class MyPluginVariable
{
    public function setTakeoverTimeout()
    {
        $cookie = new HttpCookie('takeoverViewed', true);
        $cookie->expire = time() + 3600;
        craft()->request->getCookies()->add($cookie->name, $cookie);
    }
}

and then in your template file test for (and set) the cookie using,

{% if not craft.request.getCookie('takeoverViewed') %}
    {{ setTakeoverTimeout }}
    display takeover content here
{% endif %}

Another cookie example can be seen here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.