Acess My DB service in FTl

Jamie Sammons, modified 2 Years ago. New Member Posts: 4 Join Date: 9/9/22 Recent Posts

I have added a db Bookmark and can acess it through service builder. How can I acess my service from ftl 


import ServiceBuilder.model.Bookmarks;
import ServiceBuilder.service.BookmarksLocalService

In Ftl I'm Trying like this but no results

${serviceLocator.findService("Bookmarks", "ServiceBuilder.service.BookmarksLocalService")}

thumbnail
Russell Bohl, modified 2 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts

Hi Daniya, you might need to remove serviceLocator from the Restricted Variables configuration in System Settings > FreeMarker Engine.

thumbnail
Christoph Rabel, modified 2 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts

I would recommend that you create a template context contributor to inject either a "helper" class or your service to the templates instead of enabling service locator.

https://help.liferay.com/hc/en-us/articles/360018170931-Template-Context-Contributor

Using a "helper class" (usually I call it SomethingManager) has several advantages. You can fetch data from multiple services and combine them. You also can enrich the data from the service with extra information or prepare it for easier use in the frontend. This allows you to write rather clean freemarker code without a lot of logic.

It is also better from a security point of view to avoid enabling service locator. In the last years the use of Service Locator was restricted more and more (which is a good thing, IMHO, but of course, it forces you to settle your technical debt in that regard and convert everything to context contributors)