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>