1

I am just experimenting with plugin development and would like to use the existing dateTime field type. For no other reason than to maintain consistency throughout my application.

Is there a way to call that field as a macro (for lack of better words) in my template or do I just view the generated source from one spot and copy/paste it into my templates?

Thank you!

3

All the default field types are exposed as template macros in /craft/app/templates/_includes/forms.html. Here's how you could easily create a DateTime input field:

{% import '_includes/forms' as forms %}

{{ forms.date( {
    id : 'myDateField',
    name : 'myDateField',
    value : myDate | default
} ) }}

Note that for the date field, value needs to be a valid DateTime object.

Other input types – such as a plain text field – are created in a similar manner:

{{ forms.text( {
    id: 'myTextField',
    name: 'myTextField',
    value : myValue | default,
    placeholder : 'My Placeholder',
} ) }}

Also note that as _includes/forms is outside the frontend templates folder, it will not be easily accessible outside the CP. If you want to include the form macros in a frontend template, see the following SE thread – I'm partial to Mike Pepper's answer, myself.

1
  • Fantastic! I figured there was a way other than copy/paste html. – Damon Apr 3 '15 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.