Qute and dynamic parameter

188 views
Skip to first unread message

jems....@gmail.com

unread,
Oct 16, 2022, 11:21:51 AM10/16/22
to Quarkus Development mailing list
Hi,

I have this case where i have this extension called "field"

public class FieldTemplateExtension {
  static final String FIELD = "field";

  /**
   * Can be called like this {field_name:email}.
   *
   * @see FieldName
   * @param propertyName the field enum name
   * @return the field name
   */
  @TemplateExtension(namespace = FIELD, matchName = ANY)
  static Object getConfigProperty(String propertyName) {
    return FieldName.valueOf(propertyName.toUpperCase()).toString();
  }
}


The idea behind this is that i keep an enum of field names that i use to name form fields instead of using hard coded values that can be hard to change after.

So on the backend side i use this enum for instance to retrieve a parameter value that has been posted by the user and on the front side in Qute i use them in my html templates to give a name to an input field for instance

<input type="email" name="{field.email}" />

This works correctly but i want to be able to do also is to be able to also inject some parameters and be able to keep the same logic to test for their value/existance without using a hard coded parameter name in Qute template

I tried this

{#let activated=field:activated}
...
{/let}


But the issue when doing this is that field:activated will be translated to the parameter name and that's it where in my case i want to also retrieve its value if its defined using this enum. Is this possible ? is there another approach, a cleaner one maybe ?

Thank you

Regards,

Martin Kouba

unread,
Oct 17, 2022, 4:12:21 AM10/17/22
to jems....@gmail.com, Quarkus Development mailing list
On 16. 10. 22 17:21, jems....@gmail.com wrote:
> Hi,
>
> I have this case where i have this extension called "field"
>
> /public class FieldTemplateExtension {
>   static final String FIELD = "field";
>
>   /**
>    * Can be called like this {field_name:email}.
>    *
>    * @see FieldName
>    * @param propertyName the field enum name
>    * @return the field name
>    */
>   @TemplateExtension(namespace = FIELD, matchName = ANY)
>   static Object getConfigProperty(String propertyName) {
>     return FieldName.valueOf(propertyName.toUpperCase()).toString();
>   }
> }/
>
> The idea behind this is that i keep an enum of field names that i use to
> name form fields instead of using hard coded values that can be hard to
> change after.
>
> So on the backend side i use this enum for instance to retrieve a
> parameter value that has been posted by the user and on the front side
> in Qute i use them in my html templates to give a name to an input field
> for instance
>
> <input type="email" name="{field.email}" />
>
> This works correctly but i want to be able to do also is to be able to
> also inject some parameters and be able to keep the same logic to test
> for their value/existance without using a hard coded parameter name in
> Qute template

I'm not quite sure I fully understand your question but you might define
you template extension method like this:

@TemplateExtension(namespace = FIELD)
static Object get(String propertyName) {
return FieldName.valueOf(propertyName.toUpperCase()).toString();
}

And then in the template use: {field:get(nameOfTheField)}.

Note that "get" is derived from the method name by default. And the
"nameOfTheField" is evaluated first and then passed to the template
extension method as the "propertyName" argument.

>
> I tried this
>
> {#let activated=field:activated}
> ...
> {/let}
>
> But the issue when doing this is that field:activated will be translated
> to the parameter name and that's it where in my case i want to also
> retrieve its value if its defined using this enum. Is this possible ? is
> there another approach, a cleaner one maybe ?
>
> Thank you
>
> Regards,
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com <https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Martin Kouba
Software Engineer
Red Hat, Czech Republic

jems....@gmail.com

unread,
Oct 17, 2022, 4:42:29 AM10/17/22
to Quarkus Development mailing list
No what im trying to do is the following

In my controller i do this

Map<String, Object> templateData = new HashMap<>();
templateData.put(FieldName.ACTIVATED, true);
template.data(templateData)


But then in my Qute template i want basically read what's inside the activated parameter but using the enum to access the value

If i do this the check param will be set the enum string

{#let check=field:activated}
...
{/let}

But what i want is to retrieve the value of that parameter, in this case i want check param to contain true, so i can use it later on.

I hope it's clearer. Thank you for your help

Martin Kouba

unread,
Oct 17, 2022, 5:29:34 AM10/17/22
to jems....@gmail.com, Quarkus Development mailing list
On 17. 10. 22 10:42, jems....@gmail.com wrote:
> No what im trying to do is the following
>
> In my controller i do this
>
> Map<String, Object> templateData = new HashMap<>();
> templateData.put(FieldName.ACTIVATED, true);
> template.data(templateData)
>
>
> But then in my Qute template i want basically read what's inside the
> *activated* parameter but _using the enum_ to access the value

Ok, so you'd like to use the enum constant as a key in the map.

In that case, something like this should work:

template.data("fields", templateData);

and then...

{#let check=fields.get(field:activated)}
...
{/let}

NOTE: In this case, it's better to use a specific name ("fields") for
the data map.
> https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com <https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com> <https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/quarkus-dev/c04e8d92-3d16-416f-b592-5c6b3eeea48en%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
> --
> Martin Kouba
> Software Engineer
> Red Hat, Czech Republic
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/c47ceace-7118-49d5-a08b-71b1c3a2a805n%40googlegroups.com <https://groups.google.com/d/msgid/quarkus-dev/c47ceace-7118-49d5-a08b-71b1c3a2a805n%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages