Page 1 of 1

NVelocity Category Logic

Posted: Thu Oct 29, 2009 8:35 am
by ghghgh
I see that the Category has a property that returns catalog nodes... how do I grab all the nodes that are categories or products... I want to count them so I can conditionally render user control content.

Or, what is the best way in NVelocity to count the child products and child categories of a category?

Re: NVelocity Category Logic

Posted: Thu Oct 29, 2009 8:58 am
by mazhar
It would be something like

Code: Select all

#set($productCount=0)
#set($categoryCount=0)
#foreach($node in $Category.CatalogNodes)

#if($node.CatalogNodeType=="Category")
#set($categoryCount=$categoryCount+1)
#elseif($node.CatalogNodeType=="Product")
#set($productCount=$productCount+1)
#end
#end

#if($productCount > 3)
UserControl against product count here
#elseif($categoryCount > 3 )
UserControl against category count here
#end

Re: NVelocity Category Logic

Posted: Thu Oct 29, 2009 2:36 pm
by ghghgh
Cool! Thanks.

Is it possible to user c# extension methods to add functions to the CatalogNodesCollection class so that we can do this more natively and cleanly?

Code: Select all

#if($Category.CatalogNodes.CountProducts() > 3)
#end
I've tried to do just that, but NVelocity doesn't seem to recognize them....

This is my Extension Method code (put into the App_Code folder):

Code: Select all

public static class ExtensionMethods
{
    public static int CountProducts(this CatalogNodeCollection nodes)
    {
        return CountNodeTypes(nodes, CatalogNodeType.Product);
    }

    public static int CountCategories(this CatalogNodeCollection nodes)
    {
        return CountNodeTypes(nodes, CatalogNodeType.Category);
    }

    private static int CountNodeTypes(CatalogNodeCollection nodes, CatalogNodeType nodeType)
    {
        int count = 0;
        foreach (CatalogNode node in nodes)
        {
            if (node.CatalogNodeType == nodeType)
            {
                count++;
            }
        }
        return count;
    }
}
This is my NVelocity Scriptlet code

Code: Select all

$Category.Name.ToString()  ## <-- just testing to ensure I have access to the Category

#set( $count = $Category.CatalogNodes.CountProducts() )  
$count

#set( $count = "test" )
$count
The output from the above NVelocity template is:

MyCategory $count test

Where "MyCategory" is the category name.

Also... if I change the NVelocity template to just:

Code: Select all

 $Category.CatalogNodes.CountProducts()
then the output is exactly the same....

$Category.CatalogNodes.CountProducts()

so NVelocity definitely doesn't seem to like that.


To get the extension methods to compile in the AbleCommerce Store I had to make some web.config changes....

1.) add an assembly reference to "system.core"

Code: Select all

<assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
         ...
<assemblies>
2.) tell DevStudio it should compile using C# 3.5

Code: Select all

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4"
                type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="OptionInfer" value="true"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>

Re: NVelocity Category Logic

Posted: Fri Nov 06, 2009 7:09 am
by mazhar
This is not possible, custom C# script you wrote are simply not available in NVelocity context hence this will not workout. Alternatively you can create a custom user control within ConLib folder with complete functionality then make use of that custom user control on related scriptlet. In this way you will need no NVelocity script just do all ASP.NET/C# stuff in user control and simply reference it on desired page.