How to get the page title in a Liferay velocity template - liferay

How can I get the title of current page in a CMS velocity template ?
I need the same String as is shown in the last part of the breadcrump, in other words, the page title.

Finally I found out how to do it.
It's necessary to access through the $themeDisplay
##Take layout id
#set ($layoutId = $request.get("theme-display").get("plid"))
## get the service for layout
#set($layoutService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
##convert the layout id into long
#set ($layoutLong = $getterUtil.getLong($layoutId))
##take a layout object
#set($layout = $layoutService.getLayout($layoutLong))
#set ($pageName = $layout.getName($locale))
That's it.

In a theme template: $page.getHTMLTitle($locale)
From a CMS template I'll have to dig a bit deeper for the answer... let me know if the theme is sufficient

In addition with larrytron code, It's usefull to declare this line in portal-ext.properties to use restricted variables in CMS Template
journal.template.velocity.restricted.variables=

Related

OrchardCMS, how to set HTML title tag to page title

How does one dynamically set the HTML title tag on a page in Orchard?
I have a custom theme, and out of the box Orchard sets only the sitename as the page title.
looking in the Document shape, i see this
<title>#Html.Title(title, siteName)</title>
So for example, if my site is called "MySite" and my page is called "MyPage", the title should be
<title>MySite - MyPage</title>
There is another question that seems to deal with a similar issue, though it references a Title Override module as the answer. This module no longer works with newer/current versions of Orchard.
Alternatively, I've considered just using javascript to set the title, but
a) I don't know how to get a handle on the page title and
b) I don't know which shape in which I would do this
thanks in advance
#Html.Title is just a helper, for which you can find the source code here: https://github.com/OrchardCMS/Orchard/blob/6720b71cf3474a9a7b8a8cc9a99d58b1e733acfa/src/Orchard/Mvc/Html/LayoutExtensions.cs
As you can see, it takes a variable number of parameters. If you omit the site name in your call, it won't get output.
<title>#Html.Title(title)</title>
Note that you could also simply do <title>#title</title>.

Liferay - get folder ID using folder name in velocity

I know how to get groupid in liferay velocity theme using:
#set ($scopeGroupId = $getterUtil.getLong($group_id))
However I also need to get a folder's ID using its name.
I have been researching for a while and don't seem to find a way to do that.
Q: Is this enabled and feasible in liferay 6.2?
Make sure that you are allowed to use serviceLocator or add in your portal-ext.properties this entry:
velocity.engine.restricted.variables=
After that you can use this code in your template:
#set ($folderLocalService =$serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFolderLocalService"))
#set ($folderId = $folderLocalService.getFolder($groupId,0,"folder_name").getFolderId())
$folderId
Or you can use another method from DLFolderLocalService.

Liferay Custom field from web content in velocity template

I need to set some custom tag in portal_normal.vm which define in each Web Content.
My case:
Go to Control Panel --> custom Field --> web content
Create a new custom field called "custom_metas"
Put this code in portal_normal.vm <meta property="og:title" content="$themeDisplay.getScopeGroup().getExpandoBridge().getAttribute('custom_metas')" />
This code only works if custom field is created in site (not in web content). When I create in web content show as plain text.
In my portal-ext.properties put:
journal.template.velocity.restricted.variables=
Nothing change.
Finally I try this solution given in liferay forums but doesn't work. Notice: $reserved-article-id.data doesn't print nothing.
#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($ja = $journalArticleLocalService.getArticle($getterUtil.getLong($groupId),$getterUtil.getString($reserved-article-id.data)))
#set ($resourceprimKey = $ja.getResourcePrimKey())
#set ($assetEntryLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService"))
#set ($assetEntry = $assetEntryLocalService.getEntry("com.liferay.portlet.journal.model.JournalArticle", $resourceprimKey))
#set($JournalArticleResourceLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleResourceLocalService"))
#set ($journalArticleResource = $JournalArticleResourceLocalService.getArticleResource($assetEntry.getClassPK()))
#set($JournalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($journalArticle = $JournalArticleLocalService.getArticle($getterUtil.getLong($groupId), "$journalArticleResource.getArticleId()"))
#set ($custom = $journalArticle.getExpandoBridge().getAttribute("custom_metas"))
$custom display as plain text
You are confusing theme templates with web content templates. In a theme template you have neither a single attached web content article, nor do you have access to any web content specific variable like $reserved-article-id.
And you mix up pages and portlets. Your web content article is attached to a portlet on your page - and as you can have more than one portlet with a web content article on a page, you don't have the article that is loaded.
But if you attach a tag to your webcontent (not a custom field - just a tag in the Categorization section), that tag will be added to <meta name="keywords"> in the head automatically.
If you need the tags somewhere else - they are stored in the request attribute WebKeys.PAGE_KEYWORDS (= "LIFERAY_SHARED_PAGE_KEYWORDS").

Liferay, using serviceLocator in Journal article velocity template

I am trying to use serviceLocator in the velocity template for journal article in Liferay.
I modified the portal-ext.properties file by adding line:
journal.template.velocity.restricted.variables=
Then i restarted the server
In my vm file I have:
#set ($countryService = $serviceLocator.findService("com.liferay.portal.service.CountryService"))
#set ($countryList = $countryService.getCountries())
#foreach ($country in $countryList)
$country.getName()
#end
This displays nothing in the article and also gives no error on the server. I also checked the database and countries are there and if I am using getCountries() method in my portlets service it returns countries as it should.
Has anybody had a similar problem ?
I figured out what I was doing wrong.
In the portal-ext.properties firstly I was setting:
journal.template.velocity.restricted.variables=
But instead that, it sholud be:
velocity.engine.restricted.variables=

Disable borders in Web Content Display via template

I'm creating a set of custom structures (and matching templates) for Web Content Displays on a Liferay site.
To make things more convenient for site maintainers, I would like one of these templates to suppress look-and-feel borders by default.
I've found documentation and samples online showing how to disable borders for portlets that are embedded in the theme, but I haven't had any luck applying those techniques inside a WCD template.
Here's the code I've seen embedded in a theme:
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
#set ($VOID = $theme.runtime("customportletname_WAR_pluginname", "", $velocityPortletPreferences.toString()))
$velocityPortletPreferences.reset()
Here is how I've applied it to my template:
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
<div class="custom-wcd wcdnormal">
<h2>$title.getData()</h2>
$content.getData()
</div>
$velocityPortletPreferences.reset()
I'm not surprised that it didn't work -- there's nothing analogous to the $theme.runtime call that applies the prefs to a portlet, but I also don't know whether $theme.runtime is appropriate at this layer, and if it is, what value to supply as the first argument. I suspect this last is the most likely alternative, but I don't know how to identify the WCD portlet to $theme.runtime, nor do I know how to track that information down (I'm not a Java dev by any stretch).
And just to clarify: it is not acceptable to disable borders on all portlets by default, nor am I in a position to modify the Java controller that consumes journal-article components at render-time.
Any advice is appreciated.
Simple and direct:
$velocityPortletPreferences.setValue("portletSetupShowBorders", "false")
$velocityPortletPreferences.setValue("languageIds", "pt_BR,en_US")
$velocityPortletPreferences.setValue("displayStyle", "1")
$theme.runtime("82", "", $velocityPortletPreferences.toString())
$velocityPortletPreferences.reset()
This example is for embed the language portlet (Portlet ID = 82), direct in one of .vm theme files, in my case this code is on navigation.vm.
Liferay property/preference names have a tendency to change sometimes. I created a hook this week to create a site, populate it with pages containing portlets and also to provision the CMS with a default set of structures, templates and articles. I too had to turn off the borders for my portlets and needed to set the following preference name to false to achieve that: portletSetupShowBorders
I java code it did this as follows:
PortletPreferences prefs = PortletPreferencesFactoryUtil.getLayoutPortletSetup(page, portletInstanceId);
prefs.setValue("groupId", String.valueOf(page.getGroupId()));
prefs.setValue("articleId", article.name());
prefs.setValue("portletSetupShowBorders", "false");
prefs.store();
From a Liferay Journal Template you should be able to use the following code to turn of the borders of the portlet that will show an article that uses the template:
#set ($portletPreferencesService = $serviceLocator.findService('com.liferay.portal.service.PortletPreferencesLocalService'))
#set ($companyId = $getterUtil.getLong($companyId))
#set ($ownerId = 0) ## PortletKeys.PREFS_OWNER_ID_DEFAULT
#set ($ownerType = 3) ## PortletKeys.PREFS_OWNER_TYPE_LAYOUT
#set ($plid = $getterUtil.getLong($request.theme-display.plid))
#set ($portletId = $request.theme-display.portlet-display.id)
#set ($portletPreferences = $portletPreferencesService.getPreferences($companyId, $ownerId, $ownerType, $plid, $portletId))
#set ($VOID = $portletPreferences.setValue('portlet-setup-show-borders', 'false'))
#set ($VOID = $portletPreferences.store())
Just remember that you also need to add the following line to your portal-ext.properties:
journal.template.velocity.restricted.variables=

Resources