We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multiple inheritance

Hi

I can't get work below example:

https://docs.phalcon.io/en/latest/reference/volt.html#multiple-inheritance

I have three files.

1) views/index.volt with below content:


<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        {% block stylesheet %}{% endblock %}
    </head>
    <body>
        {% block content %}{% endblock %}
        {% block javascript %}{% endblock %}
    </body>
</html>

2) views/layouts/index.volt with below content:


{% extends 'index.volt' %}

{% block stylesheet %}
    <link rel="stylesheet" href="style-layout-index.css" type="text/css" />
{% endblock %}

{% block javascript %}
    <script>alert('hello from layout');</script>
{% endblock %}

{% block content %}
<h1>Main content of view/layouts/index.volt</h1>
{% endblock %}

3) views/index/index.volt content is:


{% extends 'layouts/index.volt' %}

{% block stylesheet %}
    <link rel="stylesheet" href="style-view-index.css" type="text/css" />
{% endblock %}

{% block javascript %}
    <script>alert('hello from view');</script>
{% endblock %}

{% block content %}
<h1>Main content of view/index/index.volt...</h1>
{% endblock %}

HTML generated it's:


<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>

    </head>
    <body>

    </body>
</html>

I notice that there is not {{ content() }} called anywhere on that example, and when I try add it then code generated it's a bit messed.

Any suggestion?

I would like to have a master views/index.volt template with different blocks, which can be extended by single layouts, and each view file should be also able extend blocks of master and layout template.

Thanks!!



8.3k
Accepted
answer

I found a workaround. Can someone tell me if is good practice? Or how to manage a structure like this?

1) file view/index.volt with content:


{{ content() }}

2) file templates/index.volt with content: (I should rename folder 'layouts' to 'templates' because don't work when folder it's named 'layouts', not sure why)

<!DOCTYPE html>
<html>
    <head>
        <title>Title</title>
        {% block stylesheet %}
        <link rel="stylesheet" href="style-tpl-index.css" type="text/css" />
        {% endblock %}
    </head>
    <body>
        {% block content %}{% endblock %}
        {% block javascript %}{% endblock %}
    </body>
</html>

3) view/index/index.volt with content:


{% extends 'templates/index.volt' %}

{% block stylesheet %}
    <link rel="stylesheet" href="style-view-index.css" type="text/css" />
{% endblock %}

{% block javascript %}
    <script>alert('hello from view');</script>
{% endblock %}

{% block content %}

{{ super() }}

<h1>Main content of view/index/index.volt...</h1>
{% endblock %}