How can I use regular expressions for string matching in HTL

Avatar

Avatar

jkpanera

Avatar

jkpanera

jkpanera

13-05-2019

How can I match a string against a regular expression in HTL?

I would want to do one thing if it matches and another if it doesn't.

Alternatively, if HTL does not have that capability built in, is there a way that I can pass a string into a utility method to do that?

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838
Arun_Patidar
MVP

14-05-2019

Not with HTL but with Java(Sling Model).

Use HTL templates(not page template) to render HTML based on condition.

Answers (5)

Answers (5)

Avatar

Avatar

jkpanera

Avatar

jkpanera

jkpanera

13-05-2019

I do mean in the component code (not in a page template).

Let me give you a use case. We have some templates that handle certain image display for various AEM models. These components should generate different markup depending on the type of image (for example, we want to lazy load PNGs but not JPGs).

Is this possible to do using HTL?

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Reply

838
Arun_Patidar
MVP

13-05-2019

Hi,

with HTL alone you can't do regex matching. You need to use JAVA i.e. Sling Model or WCMUsePojo.

and yes you can pass a parameter from HTL to Java. Please check below:

HTL introduction part 4

You can call sling model in template code as well.

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

13-05-2019

When you say Template - do you mean the component code?

Anyhow - if you want to perform this task in the HTL itself (I would recommend Java and Sling Models and use HTL as a view), - see:

htl-spec/SPECIFICATION.md at master · adobe/htl-spec · GitHub

Avatar

Avatar

jkpanera

Avatar

jkpanera

jkpanera

13-05-2019

What if I don't have a sling model in the HTL where I need the reg ex functionality? For example in a template?

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

13-05-2019

If you use a Sling Models with your HTL - you can use Java to perform all REG EX functionality.