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

Extending Volt Views -- Help!

I am trying to use a bootstrap navbar as my main template, the main index.volt as a parent view, and a child, such as app/views/index/index.volt. So here is my parent view, layouts/main.volt, with twitter bootstrap navbar:

# layouts/main.volt (with bootstrap navbar)
<div class="navbar navbar-inverse" role="navigation">
 <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">SeanWentz.TK</a>
        </div>
        {{ navi.getMenu() }} //a function to spawn menu items
    </div>    
</div>
</html>

my app/views/index.volt:

# app/views/index.volt with volt parent blocks
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        {{ stylesheet_link('css/lavish-bootstrap.css')}}
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" contet="Sean Wentz">
        <title>{% block title %}{% endblock %} | SeanWentz.TK</title>
    </head>
    <body>

        {{ content() }}
        {{ javascript_include('js/jquery-2.1.1.min.js') }}
        {{ javascript_include('js/bootstrap.min.js') }}</body>
</html>

and finally, a child view, app/views/index/index.volt:

# app/views/index/index.volt with child blocks
{% extends "index.volt%}
{% include "layouts/main.volt"%}

{% block title %}
    Home
{% endblock %}

{% block content %}

    <h1>Congratulations!</h1>

    <p>You're now flying with Phalcon. Great things are about to happen!</p>

{% endblock %}

What exactly am I doing wrong? If I remove the extends line from the last file, the page loads, but I need it in order to set the title prefix according to the page. I know I must have my code wrong somehow.

I am new to this framework, and fairly new to php as a whole. Please forgive my newbishness! All help greatly appreciated. I want to learn all I can about this framework... its great. Seriously great.

Thanks again!



1.2k

I'm not sure whether or not you must use all blocks into main template. Because now your content block is not used in the main layout template. Is there any error that throws to you and did you enable display_errors at all ?

I ended up just not using {% blocks %} and instead wrote a function to determine the Title. With a simple include, it loads the navbar main.volt just fine.

The hierarchy here would be index.volt > main.volt > index/index.volt, correct?



33.8k

Maybe...

{% extends "index.volt" %}

...instead of

{% extends "index.volt %}