How to pass variables to custom fields of Widget?

24 views
Skip to first unread message

Andrew Cheung

unread,
Oct 24, 2017, 10:56:42 AM10/24/17
to dotCMS User Group

Hi, I have defined a content type called "NewsItem Widget" with several custom fields. Then I have created 2 widget content of this content type, with different values for the custom fields. (see screenshot file called WidgetContentDefinition.png). I need to pass variables from the velocity code to one of the custom fields (i.e. the "Number of Items To Pull") field. I tried putting in ${numberToPull} (where numberToPull is a variable in the velocity code), I have also tried $render.eval, but none worked.


Any idea how I can pass a variable from velocity field to the custom field in the widget content?


Thanks.


 -Andrew

WidgetContentDefinition.png

John Michael Thomas

unread,
Oct 24, 2017, 11:23:56 AM10/24/17
to dotCMS User Group
From your description I don't fully understand what you're trying to do, but here's a couple things that hopefully will help.

1. In the `Widget Code` field of the Widget Content Type, you can reference the values of any of the other fields of the Widget Content Type using the ${fieldVarName} notation.  For an example, look at the Widget content type in the starter/demo site (the actual VTL code is in the /application/vtl/widgets/you-tube/you-tube.vtl file, but look at the `Widget Code` field in the YouTube content type to see where that's used in the content type).

2. But when you create an instance of the Widget content type you're creating a content item, not a content type.  And from your screen shot (which is showing the _content_ editing screen, not the _Content Type_ editing screen) it looks like you're trying to access the value of a field in that content item from another field in that content type, which doesn't work.  The exception is if you create a custom field (in the Widget Content Type, not in the content item) and use js/dojo code to get the value of another field; but that code is still all in the widget Content Type, not in the widget content items.

So in short, any code that accesses other fields has to exist in the widget Content Type, not in the widget content items.

Andrew Cheung

unread,
Oct 24, 2017, 12:39:53 PM10/24/17
to dotCMS User Group
Hi, John. Here's what I'm trying to do. 

1. I have created a NewsItem Widget content type. This contains a few custom fields (one of which is the "numberOfItemsToPull" field). In the widget code, I have use the custom fields using ${numberOfItemsToPull}. 
2. Then in Content, I have created 2 pieces of content of the content type "NewItem Widget" content type defined above. For each piece of content, I have entered in the values for the custom fields. e.g. for the field called "searchField", I entered in the word "categories". However, for the custom field "number Of Items to pull", I don't know at this point what the value is. This is because it is determined by the velocity code.

What I'm trying to do is : A page where it calls #dotParse(myNewsPage.vtl). The myNewsPage.vtl file will call $import.read("http://mydomain.com/api/widget/id/024b65e1-327c-4069-912b-7bd84a0ba57e) and $import.read(http://mydomain.com/api/widget/id/aba6f0a3-8174-40a2-b48d-60ec777001c3) to display the 2 pieces of content. In this myNewsPage.vtl file it contains the logic regarding what the value should be used in the "number of Items to pull" field.

In summary :

dotCMS page -> myNewsPage.vtl -> calling the 2 widget instances but needs to be able to pass the "number of Items To Pull" from a variable in the myNewsPage.vtl file


Hope this clarifies what I'd like to do. Any help is appreciated.

 -Andrew
Reply all
Reply to author
Forward
0 new messages