本文整理匯總了C#中NVelocity.App.VelocityEngine.MergeTemplate方法的典型用法代碼示例。如果您正苦於以下問題:C# VelocityEngine.MergeTemplate方法的具體用法?C# VelocityEngine.MergeTemplate怎麽用?C# VelocityEngine.MergeTemplate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類NVelocity.App.VelocityEngine
的用法示例。
在下文中一共展示了VelocityEngine.MergeTemplate方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: MergeTemplate
/// <summary>
/// Merge the specified Velocity template with the given model and write
/// the result to the given Writer.
/// </summary>
/// <param name="velocityEngine">VelocityEngine to work with</param>
/// <param name="templateLocation">the location of template, relative to Velocity's resource loader path</param>
/// <param name="encoding">encoding the encoding of the template file</param>
/// <param name="model">the Hashtable that contains model names as keys and model objects</param>
/// <param name="writer">writer the TextWriter to write the result to</param>
/// <exception cref="VelocityException">thrown if any exception is thrown by the velocity engine</exception>
public static void MergeTemplate(
VelocityEngine velocityEngine, string templateLocation, string encoding, Hashtable model, TextWriter writer) {
try {
VelocityContext velocityContext = new VelocityContext(model);
velocityEngine.MergeTemplate(templateLocation, encoding, velocityContext, writer);
} catch (VelocityException) {
throw;
} catch (Exception ex) {
throw new VelocityException(ex.ToString(), ex);
}
}
示例2: CreateSqlOutput
/// <summary>
/// Creates a velocity engine that is initiated. It loads up
/// templates from the 'Templates' folder.
/// </summary>
/// <returns></returns>
protected virtual string CreateSqlOutput(IEnumerable<Migration> migrations)
{
VelocityEngine velocityEngine = new VelocityEngine();
ExtendedProperties extendedProperties = new ExtendedProperties();
extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory + "\\Templates");
velocityEngine.Init(extendedProperties);
var context = new VelocityContext();
context.Put("migrations", migrations.OrderBy(migration => migration.MigrationDate));
var stringWriter = new StringWriter();
velocityEngine.MergeTemplate("deployment_tsql.vm", "ISO-8859-1", context, stringWriter);
return stringWriter.GetStringBuilder().ToString();
}
示例3: GetString
/// <summary>
/// 生成內容
/// </summary>
/// <returns></returns>
public string GetString()
{
VelocityEngine ve = new VelocityEngine();//模板引擎實例化
ExtendedProperties ep = new ExtendedProperties();//模板引擎參數實例化
ep.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)300); //緩存時間(秒)
ve.Init(ep);
VelocityContext vc = new VelocityContext(); //當前的數據信息載體集合
foreach (var item in Items)
{
vc.Put(item.Key, item.Value);
}
TextWriter writer = new StringWriter();
ve.MergeTemplate(TemplateName, "utf-8", vc, writer);
return writer.ToString();
}
示例4: RenderTemplate
public string RenderTemplate(string masterPage, string templateName, IDictionary<string, object> data)
{
if (string.IsNullOrEmpty(templateName))
{
throw new ArgumentException("The \"templateName\" parameter must be specified", "templateName");
}
var name = !string.IsNullOrEmpty(masterPage)
? masterPage : templateName;
var engine = new VelocityEngine();
var props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _templatesPath);
engine.Init(props);
var template = engine.GetTemplate(name);
template.Encoding = Encoding.UTF8.BodyName;
var context = new VelocityContext();
var templateData = data ?? new Dictionary<string, object>();
foreach (var key in templateData.Keys)
{
context.Put(key, templateData[key]);
}
if (!string.IsNullOrEmpty(masterPage))
{
context.Put("childContent", templateName);
}
using (var writer = new StringWriter())
{
engine.MergeTemplate(name, context, writer);
return writer.GetStringBuilder().ToString();
}
}