0

I'm very new to PHP. I have a several of sites in which the only difference is a token. I use twig templating. How can I refactor my code to don't repeat myself and make a code more correct? I assume there is a way in twig to create a base template and reuse that. How should it look in my case?

My file:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../web/assets/css/error-templates.css">
</head>
<body>
    <img class="logo" src="/assets/img/logo.png">
    <div class="container">
        <div class="wrap">  
         {# The only different is token below #}    
            ::CLOUDFLARE_ERROR_500S_BOX::
        </div>
    </div>
</body>
</html>

Another file:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../web/assets/css/error-templates.css">
</head>
<body>
    <img class="logo" src="/assets/img/logo.png">
    <div class="container">
        <div class="wrap">  
       {# The only different is token below #}
            ::ALWAYS_ONLINE_NO_COPY_BOX::
        </div>
    </div>
</body>
</html>
1
  • If the answer below satisfied your needs, please accept it as the correct answer. Commented Jan 23, 2018 at 11:19

1 Answer 1

3

Create a master template file with twig extension (eg. main.twig) containing below:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="../web/assets/css/error-templates.css">
</head>
<body>
    <img class="logo" src="/assets/img/logo.png">
    <div class="container">
        <div class="wrap">  
         {% block BodyMain %}{% endblock %}
        </div>
    </div>
</body>
</html>

and in your other pages, use the code below

{% extends "main" %}
{%block BodyMain %}
   ::ALWAYS_ONLINE_NO_COPY_BOX::
{% endblock %}

See reference twig extends documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.