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

[SOLVED] Template inheritance

Guys, I wanna use template inheritance, offered by Volt, so I've followed example from docs

"views/index.volt" contains:

{# views/index.volt #}

<!DOCTYPE html>
<html>
    <head>
        {% block head %}
            <link rel="stylesheet" href="style.css" />
        {% endblock %}
        <title>{% block title %}{% endblock %} - My Webpage</title>
    </head>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
        <div id="footer">
            {% block footer %}&copy; Copyright 2012, All rights reserved.{% endblock %}
        </div>
    </body>
</html>

"views/index/index.volt" contains:

{# views/index/index.volt #}

{% extends "index.volt" %}

{% block title %}Index{% endblock %}

{% block head %}<style type="text/css">.important { color: #336699; }</style>{% endblock %}

{% block content %}
    <h1>Index</h1>
    <p class="important">Welcome on my awesome homepage.</p>
{% endblock %}

As from example I've expected the following final output:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">.important { color: #336699; }</style>
        <title>Index - My Webpage</title>
    </head>
    <body>
        <div id="content">
            <h1>Index</h1>
            <p class="important">Welcome on my awesome homepage.</p>
        </div>
        <div id="footer">
            &copy; Copyright 2012, All rights reserved.
        </div>
    </body>
</html>

But in fact I get only "views/index.volt" contents:

<!DOCTYPE html>
<html>
    <head>

            <link rel="stylesheet" href="style.css" />

        <title> - My Webpage</title>
    </head>
    <body>
        <div id="content"></div>
        <div id="footer">
            &copy; Copyright 2012, All rights reserved.
        </div>
    </body>
</html>

What I'm missing?



2.9k

hello. I ran your code in phalcon1.2.0. I got output which you expected .

compile cache file might be a problem.



19.2k

Hi, @shuogawa. I've inspected cache file "views_index_index.volt.php" and there I can see expected output, but in browser I still get only contents of parent template "views/index.volt" (tried: clearing browser cache, using Volt with option "compileAlways" => "true"). So it looks like for some reason Phalcon not picking the child template... I'm using Phalcon 1.2.0, PHP 5.4.15, Apache 2.4.4, OS X 10.7.5, Chrome 27.0.1453.110/Safari 6.0.5; project created with Phalcon Developer Tools.



19.2k

@phalcon do I need to enable something additionally, in order to use template inheritance?



2.9k

I do not have special configuration is to use the temlate inheritance. There is a need which file is to be rendered as a view, to clarify. views_index.volt.php or views_index_index.volt.php I'm sorry but I can not imagine cause.



51.2k
Accepted
answer

Try to rename views/index.volt as views/layout.volt and {# views/index/index.volt #} will extend layout.volt. It might be a name collision . Also, be sure that the view is set to volt engine and the views dir, points to thr right path. Eg:

$di['view'] = function() {
  $view = new View();
  $view->setViewsDir(__DIR__ . '/views/default/');
  $view->registerEngines(array(
      ".volt" => 'voltService'
  ));
  return $view;
};

You don't need any special setting to enable template inheritance - it's a built-in functionality.



19.2k

@calinrada looks like you were right - it's a name collision, if I rename "views/index.volt" to "views/layout.volt" everything works as expected. Thanks for suggestion!

having read this thread would have saved me hours ... ;)