BPrepper BPrepper - 11 months ago 46
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 Source

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

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

jsFiddle demo

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

Let's say:


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

And login.html.twig:

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