Manually parsing a Widget/API in Velocity

84 views
Skip to first unread message

Mark Pitely

unread,
Oct 15, 2021, 12:48:59 PM10/15/21
to dot...@googlegroups.com
Is there a suggested way to 'manually' call an existing widget and have it be parsed?
i.e. I can usually do something like this for webcontent:
#set ($content=$dotcontent.find('UUID-identifier'))
$content.Body

for when I want to add some generic content without having a container/template relationship.


How would I accomplish this with a widget istance? 
#dotParse? $dotcontent.find?
I could call the API, I guess:  /api/widget/id/17723c01-d9ae-4ca9-b11f-6125961509c1
but the only ways I know how to do that *internally* is with $import.read() or $json.fetch. 
I've been warned against using $import against my own server.
I could add it with JS, but would prefer to get it internally. 

So, I guess my real question is what is the best practice for accessing the API via velocity on the same server? If there isn't a built-in tool to use our own API, that seems a little bit of an oversight.


Mark Pitely
Marywood University

Nathan Keiter

unread,
Oct 18, 2021, 9:07:54 AM10/18/21
to dot...@googlegroups.com
Generally speaking a widget content type is made up of two distinct parts.


1. Meta-data/config-data in the widget contentlet.

2. The rendering VTL code.


I think you could load the contentlet the way you normally load content.

Then right afterwards use a #dotParse( '/path/to/my/vtl/code.vtl' ) to drop in the rendering VTL code.


This should be doable, as it is a best practice to store all your VTL code in files anyway. (It shouldn't be in the widget.code field directly, but rather a #dotParse in the widget.code field.)


You might need to alias some of the meta-data/config-data variables if needed to make it work seamlessly.


#set ($content = $dotcontent.find( 'UUID-identifier' ) )

#set( $myField = $contentlet.myField )

#dotParse( '/application/path/to/my/vtl/code.vtl' )

Nathan I. Keiter | Lead Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: dot...@googlegroups.com <dot...@googlegroups.com> on behalf of Mark Pitely <pit...@maryu.marywood.edu>
Sent: Friday, October 15, 2021 12:48 PM
To: dot...@googlegroups.com
Subject: [dotcms] Manually parsing a Widget/API in Velocity

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
Is there a suggested way to 'manually' call an existing widget and have it be parsed?
i.e. I can usually do something like this for webcontent:
#set ($content=$dotcontent.find('UUID-identifier'))
$content.Body

for when I want to add some generic content without having a container/template relationship.


How would I accomplish this with a widget istance?
#dotParse? $dotcontent.find?
I could call the API, I guess: /api/widget/id/17723c01-d9ae-4ca9-b11f-6125961509c1
but the only ways I know how to do that *internally* is with $import.read<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fimport.read&c=E,1,M_i8LWzIjw1DY-9NzRvXIDpFgTv9u-DbIdRPqXWmVlthLmxpMABb98VcOIDlrqGZAPWPda-gUDHsOHrzmOwpXyT9LL30FxRYrMPcu1riI-N58IR55eqLiv8,&typo=1&ancr_add=1>() or $json.fetch.
I've been warned against using $import against my own server.
I could add it with JS, but would prefer to get it internally.

So, I guess my real question is what is the best practice for accessing the API via velocity on the same server? If there isn't a built-in tool to use our own API, that seems a little bit of an oversight.


Mark Pitely
Marywood University

--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,gm_JW0a63K6eOxVC0Bvu_EJBR2AmkQv5mHhG9EsXws_QJ4Intl87qSNoSJujUT02lRCvh1Rf3LIyaSbWFAdWrfrTmOeqaFnsaGsXkbpp9sw6LRq0HQ,,&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms+un...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/CAFeiKqOsicB-nTT_WnUyPdcuWpsrWt%3DSN66hR8kAGPSfk9iG_w%40mail.gmail.com<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2fCAFeiKqOsicB-nTT_WnUyPdcuWpsrWt%253DSN66hR8kAGPSfk9iG_w%2540mail.gmail.com%3futm_medium%3demail%26utm_source%3dfooter&c=E,1,QTugQWHK7GbNaiH-xR6QETeVIf_u__xpeyvaIDXcObawM10Qj_cdndxk1k4O4SpxPF2EK9gpkeQYXn4d-TOgdfy_K6akEZBUqsVMGKz4DyBj4jW0K4lO&typo=1>.

Nathan Keiter

unread,
Oct 18, 2021, 9:23:55 AM10/18/21
to dot...@googlegroups.com
This might also be an option, but you'd have to test it out. Personally, I'd prefer the method I sent previously.?

#set( $content = $dotcontent.find( 'UUID-identifier' ) )
#set( $myField = $contentlet.myField )
#parse( $content.code )


Nathan I. Keiter | Lead Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: Nathan Keiter
Sent: Monday, October 18, 2021 9:07 AM
To: dot...@googlegroups.com
Subject: Re: [dotcms] Manually parsing a Widget/API in Velocity

Mark Pitely

unread,
Oct 18, 2021, 9:34:36 AM10/18/21
to dot...@googlegroups.com
Thanks.
I'm already getting the content (with dotcontent.find) in the VTL for the widget (to access binary fields, etc) so the code is already using $var.field instead of just $field, so your method should work.
Probably just easier to write the vtl file to work when called by hand or by widget and just abstract the rest. At least I won't duplicate code that way.
To be honest, this does take quite a bit of wind out of widgets.

Maybe I can write a widget that produces javascript to API pull and display *other* widgets. I'd rather not depend on js, but it seems the best/easiest solution here.

Mark Pitely
Marywood University

--
http://dotcms.com - Open Source Java Content Management

---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/1634563431269.5205%40gettysburg.edu.

Nathan Keiter

unread,
Oct 18, 2021, 11:25:09 AM10/18/21
to dot...@googlegroups.com
For what it's worth, most of my velocity work is done via a simple #dotParse()


If I need to make it easier for content contributors, I add a simple widget they can use that does a #dotParse() in the body.


The only time I really use a proper widget content type is to organize multiple similar widgets. Basically to store config values to change behaviors of complex modular velocity code bases.

Nathan I. Keiter | Lead Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: dot...@googlegroups.com <dot...@googlegroups.com> on behalf of Mark Pitely <pit...@maryu.marywood.edu>
Sent: Monday, October 18, 2021 9:34 AM
To: dot...@googlegroups.com
Subject: Re: [dotcms] Manually parsing a Widget/API in Velocity

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
Thanks.
I'm already getting the content (with dotcontent.find) in the VTL for the widget (to access binary fields, etc) so the code is already using $var.field instead of just $field, so your method should work.
Probably just easier to write the vtl file to work when called by hand or by widget and just abstract the rest. At least I won't duplicate code that way.
To be honest, this does take quite a bit of wind out of widgets.

Maybe I can write a widget that produces javascript to API pull and display *other* widgets. I'd rather not depend on js, but it seems the best/easiest solution here.

Mark Pitely
Marywood University

On Mon, Oct 18, 2021 at 9:23 AM 'Nathan Keiter' via dotCMS User Group <dot...@googlegroups.com<mailto:dot...@googlegroups.com>> wrote:
This might also be an option, but you'd have to test it out. Personally, I'd prefer the method I sent previously.?

#set( $content = $dotcontent.find( 'UUID-identifier' ) )
#set( $myField = $contentlet.myField )
#parse( $content.code )


Nathan I. Keiter | Lead Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fwww.gettysburg.edu&c=E,1,BTTMQ-yM2dZcxZGXSp4m5yMCbk6TqFZ2zUVCc_eVTUdUmS3pVZhsAM_p0QTX5gv1sLeo-1h8t3eef8cHaj5mr9iYFbrWAe8gs-YLvXHDpg,,&typo=1><https://www.gettysburg.edu/<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fwww.gettysburg.edu%2f&c=E,1,aQ2axCwaM3Z5OO6UJd0l8CLwqxfSPuHliEllj9H8xeuJU7KHZCAWtzDS1KmjJX0KeGqEFvvDvD0BYRqGWbPn-Tp4HMnZmMn152kpwvIU589nhBtg&typo=1>>
________________________________
From: Nathan Keiter
Sent: Monday, October 18, 2021 9:07 AM
To: dot...@googlegroups.com<mailto:dot...@googlegroups.com>
Subject: Re: [dotcms] Manually parsing a Widget/API in Velocity


Generally speaking a widget content type is made up of two distinct parts.


1. Meta-data/config-data in the widget contentlet.

2. The rendering VTL code.


I think you could load the contentlet the way you normally load content.

Then right afterwards use a #dotParse( '/path/to/my/vtl/code.vtl' ) to drop in the rendering VTL code.


This should be doable, as it is a best practice to store all your VTL code in files anyway. (It shouldn't be in the widget.code field directly, but rather a #dotParse in the widget.code field.)


You might need to alias some of the meta-data/config-data variables if needed to make it work seamlessly.


#set ($content = $dotcontent.find( 'UUID-identifier' ) )

#set( $myField = $contentlet.myField )

#dotParse( '/application/path/to/my/vtl/code.vtl' )

Nathan I. Keiter | Lead Network Applications Programmer
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fwww.gettysburg.edu&c=E,1,Zli6354lPxiXiJShp77zQmWLylZetpbiwiT7JiigmItHDs3WiJT5g5RmERzge_5p0_TssXFYbgAxk-K_qlKSeAeDuSIb9_Cz8aW-Rc6lKtTLqkg9&typo=1><https://www.gettysburg.edu/<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fwww.gettysburg.edu%2f&c=E,1,hYQfboeD-Yb7eN3QiI7gIUJjY-1xlQFXlkL5bQEvrJYU9dDEfhXrNYBQNFAlrqn6m6BTCnoq59atQ6dh_oDQJXviPSMlN3gIaNOmswJ1VYSyVH-VZl6CCNmRUfU,&typo=1>>
________________________________
From: dot...@googlegroups.com<mailto:dot...@googlegroups.com> <dot...@googlegroups.com<mailto:dot...@googlegroups.com>> on behalf of Mark Pitely <pit...@maryu.marywood.edu<mailto:pit...@maryu.marywood.edu>>
Sent: Friday, October 15, 2021 12:48 PM
To: dot...@googlegroups.com<mailto:dot...@googlegroups.com>
Subject: [dotcms] Manually parsing a Widget/API in Velocity

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
Is there a suggested way to 'manually' call an existing widget and have it be parsed?
i.e. I can usually do something like this for webcontent:
#set ($content=$dotcontent.find('UUID-identifier'))
$content.Body

for when I want to add some generic content without having a container/template relationship.


How would I accomplish this with a widget istance?
#dotParse? $dotcontent.find?
I could call the API, I guess: /api/widget/id/17723c01-d9ae-4ca9-b11f-6125961509c1
but the only ways I know how to do that *internally* is with $import.read<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fimport.read&c=E,1,uZiwH_-59HA16iNAEmef6u-fD8vESBZQofx4QozNlFyASVXiNnuWk6LyeV6KfFSSkplX4typ9iC3FbAViaqndC6coQgj4fqGT_DVsmXWqPL1ndAS&typo=1&ancr_add=1><https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fimport.read&c=E,1,M_i8LWzIjw1DY-9NzRvXIDpFgTv9u-DbIdRPqXWmVlthLmxpMABb98VcOIDlrqGZAPWPda-gUDHsOHrzmOwpXyT9LL30FxRYrMPcu1riI-N58IR55eqLiv8,&typo=1&ancr_add=1>() or $json.fetch.
I've been warned against using $import against my own server.
I could add it with JS, but would prefer to get it internally.

So, I guess my real question is what is the best practice for accessing the API via velocity on the same server? If there isn't a built-in tool to use our own API, that seems a little bit of an oversight.


Mark Pitely
Marywood University

--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,mlzk8sZI_iQ8yipImAd6VIecDXeVP13lYjrniCWF3exk1YQJVOK5_S5IDPHhPYSYAP6tr0kq1DtOnT0rRPMFMaczSiOKxr2c48nVp_ScYMpiP3Lw2Kc,&typo=1><https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,gm_JW0a63K6eOxVC0Bvu_EJBR2AmkQv5mHhG9EsXws_QJ4Intl87qSNoSJujUT02lRCvh1Rf3LIyaSbWFAdWrfrTmOeqaFnsaGsXkbpp9sw6LRq0HQ,,&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms%2Bunsu...@googlegroups.com><mailto:dotcms+un...@googlegroups.com<mailto:dotcms%2Bunsu...@googlegroups.com>>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/CAFeiKqOsicB-nTT_WnUyPdcuWpsrWt%3DSN66hR8kAGPSfk9iG_w%40mail.gmail.com<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2fCAFeiKqOsicB-nTT_WnUyPdcuWpsrWt%253DSN66hR8kAGPSfk9iG_w%2540mail.gmail.com&c=E,1,x0t72_V09Zqii_t2ZJ-tfU6WwBjl2eCaMlAd-mMXlpigA8FTJxUa5mUuEfnEO-UV2fKQCykzY_K-Axvgucr-ifdbdI_TbDkaT5FPl-rZkuRyKTokUYeMT-m_s24,&typo=1><https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2fCAFeiKqOsicB-nTT_WnUyPdcuWpsrWt%253DSN66hR8kAGPSfk9iG_w%2540mail.gmail.com%3futm_medium%3demail%26utm_source%3dfooter&c=E,1,QTugQWHK7GbNaiH-xR6QETeVIf_u__xpeyvaIDXcObawM10Qj_cdndxk1k4O4SpxPF2EK9gpkeQYXn4d-TOgdfy_K6akEZBUqsVMGKz4DyBj4jW0K4lO&typo=1>.

--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,cgxZOvFuH7mlFPb55JSlwudE8BcZSMZv1Surx6dnTAASindTs48MOJBZ-6qNQHQR3v1mx3zVxcFuMpVgDkbJ3QuJfpmDYbxxTctQdDUjep_4k2bPew,,&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms%2Bunsu...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/1634563431269.5205%40gettysburg.edu<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2f1634563431269.5205%2540gettysburg.edu&c=E,1,a4wrAJjUKbDASUgYuOzf0EisOmYJcaelepEiKnnWW-cMcsASh9eHbUSYoPck4iCI-wBvgt--7vY3dRysk3ZQBnBFgVIDXRdwK7CT-KiCUUENyQ,,&typo=1>.

--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,u5HiKdvN9CNX-tjtXbeWleLX3jDzy_kGcPfE4MsL0HeV2dHHvWnz0vV_OdaZO_pMHryyNMsuxD3NPlF19SIogVbTzGNm7yX0zu6hOS8QL-U,&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms+un...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/CAFeiKqN6WAar0a_q3-2o2eZHJPxbbTy_S3Py9_CPsYmK-uU1bw%40mail.gmail.com<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2fCAFeiKqN6WAar0a_q3-2o2eZHJPxbbTy_S3Py9_CPsYmK-uU1bw%2540mail.gmail.com%3futm_medium%3demail%26utm_source%3dfooter&c=E,1,I4iKcRn3eN_zWQ2KNKU4PmAJ7gHr0I-9Ub-5cKexHQmGCPI4HGAu4X4Kcnxc0ggOvuKPImOGvnGfb1enDnJiGjl0flkuDODb3MmjJOu_ePI3dnvOf8PL&typo=1>.
Reply all
Reply to author
Forward
0 new messages