Page 1 of 1
Setting NVelocity Variables
Posted: Tue Aug 07, 2012 4:40 pm
by DG433
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
Re: Setting NVelocity Variables
Posted: Fri Aug 10, 2012 11:01 am
by mtrujillo86
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);
Re: Setting NVelocity Variables
Posted: Tue Aug 21, 2012 12:52 pm
by DG433
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?
Re: Setting NVelocity Variables
Posted: Tue Aug 21, 2012 1:06 pm
by dc8johnson
These two files are in the Able source code, in the CommerceBuilder.Web.UI\UI\WebControls\WebParts folder.
Re: Setting NVelocity Variables
Posted: Wed Aug 22, 2012 10:18 am
by mtrujillo86
You don't have to pass a database object. It can be your own custom object.
Re: Setting NVelocity Variables
Posted: Mon Oct 08, 2012 9:31 am
by DG433
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...
and the only thing that is ever output is the literal text "Your Balance: ${userbalance}"