Velocity syntax to get a field of a specific content item with content id

Hi,

I’d like to print a field data of the specific item of Content Type A in a page template for Content Type B. The field to print is not from Content Type B which is associated with the page template.

The content item would be inserted to multiple pages with the page template in multiple sites under //sites. If we change text in the content item, the change reflects to all pages which contain the content item.

I think there is a way to tweak

$sys.item.getProperty("a_field").String

to replace the sys.item part with something to specify the content ID. It looks like

$sys.item(1879656).getProperty("a_field").String

. This is not a correct syntax, but you get an idea of what I’m looking for.

I’m not sure if any binding for this content item in the page template is necessary or not. Please guide me to achieve this. If you know better ways to get the same output, please share.

Thank you,

I’m not proud to admit it, but we have done something similar. In short, our use case was to automatically populate events from a specific folder if there were less that x number of items in an event slot. Our solution was to use an autoindexer (in this case the slot “magicEvents” is a autoindexer) to specify a path and then get items from that.

So something like the following gets items of content type “myMagicContentType” in a specific folder (do note that you could make the slot only return the specific content type as opposed to checking it here):


#initslot("magicEvents" "folderpath=//Sites/mysite/magicalfolder/")##
#if($sys.currentslot.relresults.size() > 0)##
#foreach( $relresult in $sys.currentslot.relresults )##
#if($relresult.getNode().getPrimaryNodeType().getType().equals("myMagicContentType"))##
$relresult.getNode().getProperty("rx:sys_title").String##
#end## relresult.getNode
#end## foreach relresult
#endslot("magicEvents")##

I really, really, really would not code in a specific content id into your templates as that makes the template rely on the existence of a specific content id (which may be accidently deleted end user). You could say the same thing about the “magicfolder”, but it is much easier to recreate a given folder structure than it is to try to create an item with a specific content id.

Of course the above assumes that you are not putting an item of CTA in a given slot for specific content items of CTB. If so, then just using the slotmacro should get the results you want…