Twig macros vs building an extension

I am building a CMS because the world definitely needs another one. I am using Twig as the templating language. Everything is working wonderfully. I have built a suite of twig macros that make it easy to build out forms on the admin side of the CMS. Here are some examples:

{% macro input(property, args = {}) %}
    {% set uuid       = uniqid() %}
    {% set collection = args.collection|default(cms.getData("collection")) %}
    {% set object     = args.object|default(cms.getData("object")) %}

	{# args priority: args, formDefinitions, defaults #}
	{% set defs = cms.formDefinitions(property, collection, object.id|default("")) %}
	{% set args = cms.getData("fieldDefaults") | merge(defs) | merge(args) %}

	{% if args.value %}
		{% set value = args.value %}
	{% elseif object %}
		{% set value = attribute(object, property) %}
	{% elseif args.default %}
		{% set value = args.default %}
	{% endif %}

    {{ _self.fieldstart(args.field, uuid, args) }}
    <input
        id="field-{{ uuid }}"
        type="{{ args.type }}"
        name="{{ property }}"
        {% if args.minlength %}minlength="{{ args.minlength }}"{% endif %}
        {% if args.min is not empty %}min="{{ args.min }}"{% endif %}
        {% if args.max is not empty %}max="{{ args.max }}"{% endif %}
        {% if args.step %}step="{{ args.step }}"{% endif %}
        {% if args.pattern %}pattern="{{ args.pattern }}"{% endif %}
        {% if args.placeholder %}placeholder="{{ args.placeholder|e }}"{% endif %}
        {% if args.help %}aria-describedby="help-{{ uuid }}"{% endif %}
        {% if args.required %}required{% endif %}
        {% if args.disabled %}disabled{% endif %}
        {% if args.readonly %}readonly{% endif %}
		{% if value is not empty %}value="{{ value|e }}"{% endif %}
    >
    {{ _self.fieldend(uuid, args) }}
{% endmacro %}

{% macro email(property, args = {}) %}
	{# Override args with static values #}
	{% set args =  args | merge({
		field : "email",
		type  : "email",
	}) %}
    {{ _self.input(property, args) }}
{% endmacro %}

{% macro emailForm(id, args = {}, collection = "email") %}
	{{ _self.start(collection, {
		id          : id,
		helpOnHover : args.helpOnHover,
		helpOnFocus : args.helpOnFocus,
		helpStyle   : args.helpStyle,
		newAction   : args.newAction,
		editAction  : args.editAction,
	}) }}
		{{ _self.id('id', { value:id, class: "hide" }) }}
		{{ _self.email('email', args) }}
	{{ _self.end(args.save, args.delete) }}
{% endmacro %}

When I started, the macros were simple. But as I added more configuration and features, they became more cluttered. This is just a small subset as well. Now I am having my doubts. I want to convert all the macros to PHP classes and make a Twig extension to generate the HTML.

Now everything works really well as-is with the macros. But I cannot get the feeling out of the back of my head telling me that I could do better.

Has anyone been in a similar boat? What do you recommend?

Having taken the time to write this out and sleeping on it, I know the correct answer… I need to refactor all of this into a TwigExtension. Suck to have to dump 2000 lines of code, but my app will be better for it.

1 Like