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

Create themes

I need to create themes for my system, and for this I need this functionality. Volt does not support extends dynamically, how can I implement this functionality?

{% extends "@App/layout.html.twig" %}

https://symfony.com/doc/current/templating/namespaced_paths.html



13.8k

Volt does support this. Comming from Symfony I find Volt almost a one on one twin with Twig.

{% extends "layout.volt" %}

{% block title %} Project {% endblock %}
{% block pagehead %} Project - index {% endblock %}

{% block flashMessages %} {{ super() }} {% endblock %}

{% block content %}
<div class="box">
    <div class="box-header width-border">
        <a href="{{ url("project/new") }}" class="btn btn-primary pull-right primary-add"><i class="fa fa-plus"></i> <span>Create Project</span></a>
    </div>

    <div class="box-body">

        <div class="row">
            <div class="col-xs-12">

                <table id="projectTable" class="table table-bordered table-hover responsive">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Client</th>
                            <th>Name</th>
                            <th>Status</th>
                            <th>Delivered</th>
                            <th>Last Modified</th>
                            <th>Options</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for project in projects %}
                        <tr>
                            <td width="80px" >{{ project.id }}</td>
                            <td>{{ project.client_id }}</td>
                            <td>{{ project.name }}</td>
                            <td width="100px" >{{ project.project_status_id }}</td>
                            <td width="80px" >{% if project.delivered %} Yes {% else %} No {%  endif %}</td>
                            <td width="150px" >{{ project.modified }}</td>
                            <td width="120px">
                                <a class="btn btn-default btn-sm" href="{{ url("timeregistration/new/" ~ project.id) }}" title="add time" ><i class="fa fa-plus"></i></a>
                                <a class="btn btn-default btn-sm" href="{{ url("project/edit/" ~ project.id) }}" title="update" ><i class="fa fa-pencil"></i></a>
                                <a class="btn btn-default btn-sm" href="{{ url("project/confirm/" ~ project.id) }}" title="delete" ><i class="fa fa-trash"></i></a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                    <tfoot>
                        <tr>
                            <th>#</th>
                            <th>Client</th>
                            <th>Name</th>
                            <th>Status</th>
                            <th>Delivered</th>
                            <th>Last Modified</th>
                            <th>Options</th>
                        </tr>
                    </tfoot>
               </table>

            </div>
        </div>

    </div>
</div>
{% endblock %}

{% block javascripts %}
<script type="text/javascript">
    $(function(){
        $("#projectTable").DataTable();
    });
</script>
{% endblock %}

I mean to extend dynamically. Look at the symbol @ in extends



3.4k
edited May '17

Have you tried something like this :

{% extends router.getNamespace() ~ "/layout.html.twig" %}

or something like that :

{{ set namespace = router.getNamespace() }}
{% extends  namespace ~ "/layout.html.twig" %}

https://forum.phalcon.io/discussion/2131/get-controller-and-action-name-in-a-volt-template-



13.8k
edited May '17

I mean to extend dynamically. Look at the symbol @ in extends

Have you tried a variable in extending?

Or do you mean that you want to include a part which is a result of a action dynamically ? I think partials is used for that in which you could also use variables

{{ partial('user/info',['some_name':'some_value'] }}
edited Jun '17

I'm completely new to Phalcon and was wondering the same thing. Having used Twig, what is needed is the ability to specify multiple view directories using a named key which can then be referenced in Volt using a defined prefix like @.

Obviously the example below will not work because Views in Phalcon only accept a single path (as far as I can tell). But you get the idea?

$container->set('view', function()
    {
        $view = new View();
        $view->setViewsDir('global', 'global/views/');
        $view->setViewsDir('project', 'project/views/');
        $view->registerEngines(
            [
                '.template' => 'voltService',
            ]
        );

        return $view;
    }
);
{% extends "@project:layout.html.twig" %}

To find out more see the documentation under loaders: https://twig.sensiolabs.org/doc/2.x/api.html#twig-loader-filesystem