Skip to content

Field name aliases in template context #154

@Toutouwai

Description

@Toutouwai

Short description of the enhancement

When setting up fields for a project, there's always a trade-off between code readability and efficient use of fields.

In one extreme you create new fields for every usage in every template and name the fields according the usage. So if in template "person" you need a field for age and in template "walk" you need a field for distance you then you create separate integer fields "age" and "distance". The pro here is that your field names are self-explanatory when used in template files, but the con is an unnecessarily large number of fields.

In the other extreme you only ever create the minimum number of fields with generic names like "text_1", "integer_1", etc. So in the example above you use field integer_1 in both person and walk templates. The pro here is that your field use is very efficient, but the con is that your field names are meaningless within your template files, and you find yourself needing to add comments every time the fields are used.

I have an idea that might allow the best of both worlds but I'm not sure about the ideal way to implement it. So this is just one way it could be done...

Fields could be given "aliases" in template context. So when I add the field integer_1 to my person template, I edit the field in template context from the template editor and give it the alias "age". Now when I want to get the field value in my template file I can get it using the alias plus some special symbol (e.g. *) to indicate that I am using an alias name.

echo $page->*age; // the same as $page->integer_1

So when PW is asked for a field name preceded by an asterisk it knows to check for a field with that alias in $page's template context.

And perhaps if you needed to get a field by alias independent of a template context you could supply the template name before the asterisk.

$field = $fields->person*age; // the same as $fields->integer_1

So, in general, PW would give special treatment when asked to deal with a field name that contains an asterisk.

Having a feature like this would allow for the efficient use of fields and more readable code without needing a lot of explanatory comments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions