BPrepper BPrepper - 3 months ago 5
CSS Question

Change text in div depending on what page a user is

I want to change the text in my footer depending on what page a user is.
I use a global footer.php and header.php file.

The goal is that if a user is on index.php that the div in the footer displays no text. But when a user goes to login.php the div would display "Back to home" in the footer.

I've tried doing this using PHP or jquery, but I did not find anything on google or got anything to work.

Is this even possible or not and how would I go about doing this (Javascript, PHP, Jquery, ...?)

Answer

Simple way:
In footer.php place
<div id="footer"></div>
In login.php place

<script>
$(function(){
   $('#footer').html('<a href="index.php">Back to home</a>');
});
</script>

jsFiddle demo

But the better way would be to use Twig with its block extending.

Let's say:

base.html.twig:

<!DOCTYPE html>
<html>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
        <div id="footer">
            {% block footer %}
            {% endblock %}
        </div>
    </body>
</html>

And login.html.twig:

{% extends "base.html.twig" %}
{% block footer %}
    <a href="...">Back to home</a>
{% endblock %}
Comments