Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Using JQuery in a Transformation View modes: 
User avatar
Member
Member
Armysniper89 - 12/6/2010 3:02:13 PM
   
Using JQuery in a Transformation
I am trying to customize a transformation for the Article document type so that it will insert a custom field's contents and insert a graphic before the custom field ONLY when the custom field has a value specified. I figured the best way to do this would be to have a little JQuery script insert it when there is a value specified for the field. The question is, is it possible to include JQuery library code in a transformation?

User avatar
Kentico Consulting
Kentico Consulting
kentico_mirekr - 12/6/2010 3:47:05 PM
   
RE:Using JQuery in a Transformation
Hi,

Yes, you can use any code in your transformation code according your needs. However, first of all, I would recommend you to register the jQuery on your page. You can register the jQuery with JavaScript web part.

This web part has Linked file property where you can add link to Kentico CMS jQuery library:

Linked file: ~/CMSScripts/jquery/jquery-core.js

Or, you can register the jQuery in HEAD HTML web part or in head section of your master page template or page template or with following code:

CMS.GlobalHelper.ScriptHelper.RegisterJQuery(Page) 

public static void RegisterJQuery(
Page page
)


For more information, please refer to our API reference guide:

http://devnet.kentico.com/downloads/kenticocms_api.zip

Best regards,
Miro Remias.

User avatar
Member
Member
Armysniper89 - 12/7/2010 8:55:54 AM
   
RE:Using JQuery in a Transformation
Thanks Miro,

I ended up doing my work by using a macro in the transformation to look for the field being empty and if it was not, inserting my HTML. With that said, I know there will be times when I want to use JQuery in my work. What if I have the JQuery library loaded into my master page? I should be able to simply create my functions right?

User avatar
Kentico Consulting
Kentico Consulting
kentico_mirekr - 12/9/2010 8:12:59 AM
   
RE:Using JQuery in a Transformation
Hi,

Yes, you can also load your jQuery library or Kentico CMS jQuery library in your master page template head section. Then you can work with the jQuery library the standard way and use it anywhere in the page – for example in repeater’s transformation code.

Best regards,
Miroslav Remias

User avatar
Member
Member
Case - 12/31/2010 12:56:48 AM
   
RE:Using JQuery in a Transformation
Would you mind sharing the macro you used? I'm working on a similar problem, only I'm trying to hide some elemets on a document based on if a field is empty.

User avatar
Member
Member
Case - 12/31/2010 1:04:50 AM
   
RE:Using JQuery in a Transformation
Let me clarify;

I have an editable text webpart that I use in a custom document type. One of the fields in this doctype is an image. I also have some HTML in the HTML Envelope of the editable text web part that will display the image. However, I want to be able to hide this extra html if the image is empty. What would be the best way to do this?

I was thinking I could do something with Macros, but I wasn't able to figure out a solution, so I decided to try to use javascript/jquery, but I'm having issues with getting that to work.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 12/31/2010 3:04:05 AM
   
RE:Using JQuery in a Transformation
Hi,

you can set editable text webpart visible only for your custom document type, for example:
Show for document types: CMS.Article

Now you can insert your custom macro into Content before and Content after property:
{#someexpresion#}

And code of your custom macro could look like:


switch (expression.ToLower())
{
case "someexpression":
match = true;
// image is the name of image field in document type
if (String.IsNullOrEmpty(CMSContext.CurrentDocument.GetValue("image").ToString()))
{
result = "<b> empty </b>";
}
else
{
result = "<i> is not empty empty </i>";
}
break;
}


Best regards,
Ivana Tomanickova