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

Question...volt code or php code on volt file ??

Hi, i have a question, i see that when use volt template, a new file was created with php extension, example:

volt file: ( login.volt)

<script type="text/javascript">
var s_user_required ="{{ _('Usuario requerido') }}";
var s_pass_required = "{{ _('Password requerido') }}";
</script>
<div class="container">
    <div class="card card-container">
        <form id="form_login" class="form-signin">
            <h2 class="form-signin-heading">{{ _('Inicio de sesion') }}</h2>
            <input type="text" id="user_name" name="user_name" class="form-control" placeholder="{{ _('Ingrese su Usuario') }}" required autofocus>
            <input type="password" id="user_password" name="user_password" class="form-control" placeholder="{{ _('Ingrese su password') }}" required>
            <div id="user_remember" class="checkbox">
                <label>
                    <input type="checkbox" name="user_rememberme" value="1">{{ _('Recordarme') }}
                </label>
            </div>
            <button id="btn_process_login" class="btn btn-lg btn-primary btn-block btn-signin" type="submit">{{ _('Iniciar Sesion') }}</button>
        </form>
        <a href="#" class="forgot-password">
            {{ _('Olvidaste tu password?') }}                
        </a>
        <div id="message_login"></div>
    </div>
</div>

and the new php extension volt file : ( login.volt.php)

<script type="text/javascript">
var s_user_required ="<?php echo gettext('Usuario requerido'); ?>";
var s_pass_required = "<?php echo gettext('Password requerido'); ?>";
</script>
<div class="container">
    <div class="card card-container">
        <form id="form_login" class="form-signin">
            <h2 class="form-signin-heading"><?php echo gettext('Inicio de sesion'); ?></h2>
            <input type="text" id="user_name" name="user_name" class="form-control" placeholder="<?php echo gettext('Ingrese su Usuario'); ?>" required autofocus>
            <input type="password" id="user_password" name="user_password" class="form-control" placeholder="<?php echo gettext('Ingrese su password'); ?>" required>
            <div id="user_remember" class="checkbox">
                <label>
                    <input type="checkbox" name="user_rememberme" value="1"><?php echo gettext('Recordarme'); ?>
                </label>
            </div>
            <button id="btn_process_login" class="btn btn-lg btn-primary btn-block btn-signin" type="submit"><?php echo gettext('Iniciar Sesion'); ?></button>
        </form>
        <a href="#" class="forgot-password">
            <?php echo gettext('Olvidaste tu password?'); ?>                
        </a>
        <div id="message_login"></div>
    </div>
</div>

So, my question is......why use volt code? and no php code on volt file?.....

i can use php code on volt file? (login.volt), the performance is affected at some point?? or not?

Volt makes your templates more readable and allows you to extend them, and yes, you can embed PHP code in Volt.

About performance Volt is very very fast so I don't think that can affect the performance of your application significantly, at least not much more than a pure PHP template, which is what volt is once compiled.

thanks, so I can use php code inside volt, and there isnt problem, the performance is not affected?, for example if statements, foreach, in_array .... thanks for your support!

if you want to use only php code, better use .phtml, otherwise better use only volt syntax

edited Apr '15

one more question....... if i use .phtml , Template Inheritance works?, sorry iam new in this subject...

finally,

1) i can use php code embed into volt extension with volt code ........without any problem?

2) only the different on use volt template is for code designer?, sintax?

3) If I focus only on performance is better to use phtml, because is plain php code?

4) if i will use .phtml extension, Template Inheritance works like volt template?

Thanks for yours anwers!, with this'll clear my doubt!



16.0k
Accepted
answer
edited Apr '15

1) yes you can, but there will be no syntax highlighting and I advice against it. Keeping php syntax out of volt files will make your code more readable and cleaner. You'll find a volt syntax for everything you need

2) a template engine helps in making the code more readable and also helps to write views faster. But will be quite slow until you learn it. Volt syntax is the same as Twig; so, if you learn one, you learn both.

3) volt is written in C and integrated in phalcon; also if you take a look, all volt files are compiled and then used the compiled version. You have nothing to worry about performance from this side

4) volt syntax is being compiled to phtml code, so everything that works in volt, work in phtml too; including template inheritance. Just check the compiled volt files to see the result

Thanks a lot for your anwers!