DotNetNuke 类库之DotNetNuke.Services.Tokens

该类提供:关键字(token)替换的方法,

默认支持 User, Host, Portal, Tab, Module, Membership, Profile,
                            Row, Date,  ArrayList (Custom), IDictionary

替换文字的写法为[object:property] or [object:property|format]

例如:

日期:[Date:Current|yyyy/MM/dd]

亲爱的[User:FullName]

网址: [Portal:URL]
用户名: [Membership:UserName]

替换后为:

日期: 2009/06/07

亲爱的SuperUser Account

网址: 492.xakep.com
用户名: host

代码:

string strToken = Localization.GetString("Token", this.LocalResourceFile);

string strTokenReplaceValue=Null.NullString;    

DotNetNuke.Services.Tokens.TokenReplace objTokenReplace = new DotNetNuke.Services.Tokens.TokenReplace();
objTokenReplace.AccessingUser = UserInfo;
objTokenReplace.ModuleInfo = ModuleConfiguration;
objTokenReplace.ModuleId = ModuleId;
objTokenReplace.PortalSettings = PortalSettings;
objTokenReplace.User = UserInfo;
objTokenReplace.Language = CultureInfo.CurrentCulture.Name;

objTokenReplace.DebugMessages = !(DotNetNuke.Common.Globals.IsTabPreview());

strTokenReplaceValue= objTokenReplace.ReplaceEnvironmentTokens(strToken );

重点说下如何支持  Row,  ArrayList (Custom), IDictionary的替换

1、Row的支持

使用ReplaceEnvironmentTokens(string, System.Data.DataRow)

例如:

列1:[row:aa1]
列2:[field:aa2]

替换后

列1:aa11
列2:aa22

代码:

lblToken.Text = Localization.GetString("Token", this.LocalResourceFile);

DotNetNuke.Services.Tokens.TokenReplace objTokenReplace = new DotNetNuke.Services.Tokens.TokenReplace();
objTokenReplace.AccessingUser = UserInfo;
objTokenReplace.ModuleInfo = ModuleConfiguration;
objTokenReplace.ModuleId = ModuleId;
objTokenReplace.PortalSettings = PortalSettings;
objTokenReplace.User = UserInfo;
objTokenReplace.Language = CultureInfo.CurrentCulture.Name;

objTokenReplace.DebugMessages = !(DotNetNuke.Common.Globals.IsTabPreview());

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("aa1");
dt.Columns.Add("aa2");

DataRow dr = dt.NewRow();
dr["aa1"] = "aa1";
dr["aa2"] = "aa2";

lblTokenValue.Text = objTokenReplace.ReplaceEnvironmentTokens(lblToken.Text, dr);

2、ArrayList 的支持

使用ReplaceEnvironmentTokens(string, System.Collections.ArrayList, string)

例如:

1:[Token:0]
2:[Token:1]

替换后

1:00
2:11

代码:

lblToken.Text = Localization.GetString("Token", this.LocalResourceFile);

DotNetNuke.Services.Tokens.TokenReplace objTokenReplace = new DotNetNuke.Services.Tokens.TokenReplace();
objTokenReplace.AccessingUser = UserInfo;
objTokenReplace.ModuleInfo = ModuleConfiguration;
objTokenReplace.ModuleId = ModuleId;
objTokenReplace.PortalSettings = PortalSettings;
objTokenReplace.User = UserInfo;
objTokenReplace.Language = CultureInfo.CurrentCulture.Name;

objTokenReplace.DebugMessages = !(DotNetNuke.Common.Globals.IsTabPreview());

ArrayList al = new ArrayList();
al.Add("00");
al.Add("11");

lblTokenValue.Text = objTokenReplace.ReplaceEnvironmentTokens(lblToken.Text, al ,”Token”);

注:红色的“Token”要保持一致

3、IDictionary支持

使用ReplaceEnvironmentTokens(string, System.Collections.IDictionary, string)

例如:

1:[Token:Token0]
2:[Token:Token1]

替换后

1:Token00
2:Token11

代码:

lblToken.Text = Localization.GetString("Token", this.LocalResourceFile);

DotNetNuke.Services.Tokens.TokenReplace objTokenReplace = new DotNetNuke.Services.Tokens.TokenReplace();
objTokenReplace.AccessingUser = UserInfo;
objTokenReplace.ModuleInfo = ModuleConfiguration;
objTokenReplace.ModuleId = ModuleId;
objTokenReplace.PortalSettings = PortalSettings;
objTokenReplace.User = UserInfo;
objTokenReplace.Language = CultureInfo.CurrentCulture.Name;

objTokenReplace.DebugMessages = !(DotNetNuke.Common.Globals.IsTabPreview());

Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Token0", "Token00");
dic.Add("Token1", "Token11 ");

lblTokenValue.Text = objTokenReplace.ReplaceEnvironmentTokens(lblToken.Text, dic,”Token”);

posted @ 2009-06-13 22:10  炽焰惊鸿  阅读(267)  评论(0编辑  收藏  举报