NVelocity Category Logic

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
Post Reply
ghghgh
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Fri Oct 09, 2009 12:21 pm

NVelocity Category Logic

Post by ghghgh » Thu Oct 29, 2009 8:35 am

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?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: NVelocity Category Logic

Post by mazhar » Thu Oct 29, 2009 8:58 am

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

ghghgh
Ensign (ENS)
Ensign (ENS)
Posts: 11
Joined: Fri Oct 09, 2009 12:21 pm

Re: NVelocity Category Logic

Post by ghghgh » Thu Oct 29, 2009 2:36 pm

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>

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: NVelocity Category Logic

Post by mazhar » Fri Nov 06, 2009 7:09 am

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.

Post Reply