Setting NVelocity Variables

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
DG433
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Tue Aug 07, 2012 2:04 pm

Setting NVelocity Variables

Post by DG433 » Tue Aug 07, 2012 4:40 pm

I have read through nearly every post here regarding NVelocity, and taken a good bit of ideas.

However, I am struggling with setting a new variable to the NVelocity engine, I feel I am missing how to commit it to the NVelosity engine after I add new variables. I am trying to simply allow me to read certain elements in scriplets using the NVelocity variables that I set previously (during the login process). I have read through the forums but no luck. Additionally the NVelosity documentation is lacking..

Code: Select all

                        
NVelocityEngine nve = NVelocityEngine.Instance;
Hashtable parameters = new Hashtable();
parameters.Add("userbalance", "100");

//How do I "commit" this addition here?
When I try to display the ${userbalance}, I simply get it in text format on the webpage "${userbalance}". When I change it to a ${customer.PrimaryAddress.FirstName}, then the first name appears fine.

Code: Select all

#if(!$customer.IsAnonymous)
Your Balance: ${userbalance}
#end
Thanks

mtrujillo86
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 34
Joined: Wed Feb 29, 2012 4:45 pm

Re: Setting NVelocity Variables

Post by mtrujillo86 » Fri Aug 10, 2012 11:01 am

There are two files that you could add custom objects too:
Scriptletpart.cs
scriptletpartex.cs

You can't just add a "100", it has to be an object of some sort.

For instance:

object store = Token.Instance.Store;
object customer = Token.Instance.User;
parameters.Add("store", store);
parameters.Add("customer", customer);
parameters.Add("page", this.Page);
//TODO: OBSOLETE PARAMETERS, REMOVE FOR AC7.1
parameters.Add("Store", store);
parameters.Add("User", customer);
DateTime Now = new DateTime();
parameters.Add("DateTime", Now);

//CHECK FOR CATEGORY
int categoryId = GetCategoryId();
Category category = CategoryDataSource.Load(categoryId);
if (category != null) parameters.Add("Category", category);

//CHECK FOR PRODUCT
int productId = GetProductId();
Product product = ProductDataSource.Load(productId);
if (product != null) parameters.Add("Product", product);

//CHECK FOR WEBPAGE
int webpageId = GetWebpageId();
Webpage webpage = WebpageDataSource.Load(webpageId);
if (webpage != null) parameters.Add("Webpage", webpage);

//CHECK FOR LINK
int linkId = GetLinkId();
Link link = LinkDataSource.Load(linkId);
if (link != null) parameters.Add("Link", link);

DG433
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Tue Aug 07, 2012 2:04 pm

Re: Setting NVelocity Variables

Post by DG433 » Tue Aug 21, 2012 12:52 pm

Thanks, I'll try just passing an object from the database.

Also, I am unable to find the scripletpart.cs and scriptletpartex.cs files you are referring to?

dc8johnson
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 87
Joined: Fri Nov 20, 2009 8:46 am

Re: Setting NVelocity Variables

Post by dc8johnson » Tue Aug 21, 2012 1:06 pm

These two files are in the Able source code, in the CommerceBuilder.Web.UI\UI\WebControls\WebParts folder.
David Johnson

mtrujillo86
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 34
Joined: Wed Feb 29, 2012 4:45 pm

Re: Setting NVelocity Variables

Post by mtrujillo86 » Wed Aug 22, 2012 10:18 am

You don't have to pass a database object. It can be your own custom object.

DG433
Ensign (ENS)
Ensign (ENS)
Posts: 7
Joined: Tue Aug 07, 2012 2:04 pm

Re: Setting NVelocity Variables

Post by DG433 » Mon Oct 08, 2012 9:31 am

Thanks for the feedback guys. I have tried now for some time to get it to work (eventually moved on to other portions), but now coming back to this.

I apologize for not "getting it", but I am still just lost...

I have tried numerous methods, from creating custom objects etc. and still cannot get a result (though I don't know if I am calling it properly)...

I have tried things like this...

Code: Select all

 private object StringToObject(string base64String)
    {
        return null;
        
        byte[] bytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
        ms.Write(bytes, 0, bytes.Length);
        ms.Position = 0;
        return new BinaryFormatter().Deserialize(ms);
        
    }

NVelocityEngine nve = NVelocityEngine.Instance;
Hashtable parameters = new Hashtable();
object objUserBalance = StringToObject("100");
parameters.Add("userbalance", objUserBalance);
or like this...

Code: Select all

    public decimal _balance =0;
    public object nVelTest
    {
        get { return _balance; }
        set { _balance = value; }
    }

objBalance = new NVelTest(100);
parameters.Add("userbalance", objBalance);
in the script section (not modifying the scripletpart.cs, but rather the "Standard Header" scriplet, I have simply added...

Code: Select all

                          
Your Balance: ${userbalance}
and the only thing that is ever output is the literal text "Your Balance: ${userbalance}"

Post Reply