Is that it is possible embed web content in a template velocity?
I have two web contents and I want to unite the two into a single one.
I tried this:
#set ($webcontent-id = "13054")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id,null,"$locale",$theme_display))
<div> $webcontent </div>
#set ($webcontent-id = "13065")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id,null,"$locale",$theme_display))
<div> $webcontent </div>
However, it displays the variable. I think I did not access the service in a webcontent.
Web content templates do not have access to the ThemeDisplay directly. They can access request information via the $request map, which contains among other things variables from ThemeDisplay. This wiki page lists the variables that can be used from templates.
Also, when calling Liferay services from velocity templates you need to make sure that all arguments have the correct type. You can use $getterUtil to accomplish this, for example to convert a String to Long.
Here's a revised version of your example:
#set ($group_id = $getterUtil.getLong($request.theme-display.scope-group-id))
#set ($webcontent-id = "58007")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id, "", "$locale", ""))
$webcontent
Related
I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
The select list will use ?StockIndexId=1
The Create New link will use /1, when returning to the Index /1 is used
If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
#page "{StockIndexId?}"
#model Investments.Pages.StockIndexMonths.IndexModel
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="#Model.StockIndexId">Create New</a>
<table class="table">
...
Alter the form tag so that it uses the POST method:
<form method="post">
Currently, because the method is not specified, GET is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.
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").
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=
I'm creating a custom theme for Liferay, I wish to include the footer within each page layout individually. Unfortunately, I don't appear to have access to the $full_templates_path variable within the page layout files. I have had no luck manually storing the value with #set and then accessing that value within the included template.
In a vanilla theme, processing of files is something like this:
portal_normal.vm:
1) some init, doctype, etc.
2) #parse("$full_templates_path/header.vm")
3) $theme.include($content_include)
a) custom_layout_1.tpl
b) chat portlet
4) #parse("$full_templates_path/footer.vm")
For layout purposes, I need to deviate from this pattern, like so:
portal_normal.vm
1) some init, doctype, etc.
2) #parse("$full_templates_path/header.vm")
3) $theme.include($content_include)
a) custom_layout_1.tpl
i) #parse("$full_templates_path/footer.vm")
b) chat portlet
When I try this, tomcat errors out because $full_templates_path is not defined within custom_layout_1.tpl. I tried to fix this problem by doing the following within portal_normal.vm
#set($full_footer_path = "$full_templates_path/footer.vm")
$theme.include($content_include)
And then, within custom_layout_1.tpl, I do this where I'd like the footer markup emitted:
#parse("$full_footer_path")
However, tomcat still errors out, saying that $full_footer_path is not defined.
When I hard-code the value of $full_templates_path into #parse statement in custom_layout_1.tpl, everything works fine, but that seems like a hack to me.
Ideally, this should do the right thing for the right reasons, not just because I used a lot of duct tape.
Any suggestions for ways to implement the inclusion of a template file from within a custom page layout?
The issue could be due to your velocity configuration.
The following property should be false velocimacro.permissions.allow.inline.local.scope if you want to access variables set in one template to be accessible in another
You may have miss-typed your question but shouldn't #set($full_footer_path = "$full_templates_path/footer.vm" have a close bracket at the end...
#set( $full_footer_path = "$full_templates_path/footer.vm" )
It might also help to wrap $full_templates_path in curly braces to distinguish it from the rest of the text i.e ${full_templates_path}
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=