掲示板

How to add custom variables to freemarker?

7年前 に Mark Clarke によって更新されました。

How to add custom variables to freemarker?

Regular Member 投稿: 107 参加年月日: 12/03/03 最新の投稿
Hi there,

We need to add some custom variables to the freemarker context and its a bit more complicated that what can be handled by editing init-custom.ftl. I have tried by implementing ServicePreAction but the blade generated project could not find the class. I have had a look at the blade samples and the LoginPreAction example but there seems to only be events for pre/post login and pre/post service. What service and/or what event do I need to override/extend to be able to add custom variables for our theme?

thanks
7年前 に Mark Clarke によって更新されました。

RE: How to add custom variables to freemarker?

Regular Member 投稿: 107 参加年月日: 12/03/03 最新の投稿
Ok - I found this example fro TemplateContextContributor

https://github.com/liferay/liferay-blade-samples/tree/master/gradle/blade.template.context.contributor

Seems to fire multiple times when a url takes the user to a new page? Is this the correct service to implement as a OSGI component to achieve what I want? Its nice and simple - just want to check the "firing multiple times" issue and what are the recommendations for caching any queries etc that may be necessary to populate the context variables?

thanks
thumbnail
7年前 に Julio Camarero によって更新されました。

RE: How to add custom variables to freemarker?

Liferay Legend 投稿: 1668 参加年月日: 08/07/15 最新の投稿
Hi Mark,

yes, that is exactly the goal of the Context Contributors, inject anything you need to your template context (either themes or ADTs). We have a tutorial about them almost ready that will contain all the details. I will post a link here when it is published.

Regarding the fact that they are executed several times, that sounds like a bug. Could you please report it in JIRA giving more details? We should have a look if that is the case.

Regarding caching, you can use any mechanism that fits your needs. You could store things in database, you could store things in the cache utilities provided by liferay, you could store things in the elastic search engine included, you could just put your variable in a private variable... it depends on what you need to do.

Thank you!
7年前 に Peter Pan によって更新されました。

RE: How to add custom variables to freemarker?

New Member 投稿: 22 参加年月日: 15/01/14 最新の投稿
thanks Julio - will gather the necessary info and then post a bug report.
thumbnail
7年前 に Cody Hoag によって更新されました。

RE: How to add custom variables to freemarker?

Junior Member 投稿: 70 参加年月日: 12/04/26 最新の投稿
Just an FYI, the Context Contributors tutorials is now published on LDN.
6年前 に Jacopo Bartolini によって更新されました。

RE: How to add custom variables to freemarker?

Junior Member 投稿: 28 参加年月日: 17/01/19 最新の投稿
Did you reported this bug? Because I'm trying too to use this feature... I'm trying to inject some elements from some portlets that I made, to use in the meta-tags, but it does not seem to work as intended.