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

Form themes/templates

Having ditched Symfony in favor of Phalcon I have stumbled across a problem regarding form templates.

I understand for example a field is rendered in this way:

{{ text_field("name", "size" : 30) }}

{{ submit_button("Save") }}

and if I wanted to apply a class to this field it would look like:

{{ text_field("name", "size" : 30, "class" : "classForField") }}

{{ submit_button("Save", "class" : "btn btn-success") }}

However doing this for the 100's of fields, buttons etc in my app would be a massive hassle, especially if I decided for some reason to change the Css clas for just 'text_fields' for example.

In Symfony you would define a basic template for all fields in a template file which in turn would be used by all forms unless overridden.

I've looked through many of the sample projects, and through the documentation but I can't find such a feature in Phalcon. I'm hoping this is just me looking for the wrong terminology could someone point me in the right direction please.



7.9k

You mau use this : https://docs.phalcon.io/en/latest/reference/forms.html to avoid duplication for your form

You should use Phalcon Form as @Prasetyo said.

in this way you can write your element or your element rendere that affected on all your elements.

Or you can use macros

{%- macro my_input(name) %}
    {{ return text_field(name, "size" : 30, "class" : "classForField") }}
{%- endmacro %}

{# Call the macro #}
{{ '<p>' ~ my_input('name') ~ '</p>' }}

Can't for the life of me get this to work. I can't understand how I can use it to override the Default styles. Any suggestions?

You mau use this : https://docs.phalcon.io/en/latest/reference/forms.html to avoid duplication for your form

I've pretty much decided this is the way forward the only issue I'm having is Some random error about $Whatever is not Valid. I'm thinking the controller set up by the devtools plugin isn't working have I need it to so might have to strip it all out and do it by hand :/

Or you can use macros

{%- macro my_input(name) %}
   {{ return text_field(name, "size" : 30, "class" : "classForField") }}
{%- endmacro %}

{# Call the macro #}
{{ '<p>' ~ my_input('name') ~ '</p>' }}