本文整理汇总了C#中VTemplate.Engine.Template类的典型用法代码示例。如果您正苦于以下问题:C# Template类的具体用法?C# Template怎么用?C# Template使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Template类属于VTemplate.Engine命名空间,在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateExpression
/// <summary>
/// 从表达式文本中构造表达式.如果表达式是以$字符开头.并且不是以$$字符开头.则认为是变量表达式.否则则认为是常量表达式
/// </summary>
/// <param name="ownerTemplate"></param>
/// <param name="expressionText"></param>
/// <returns></returns>
internal static IExpression CreateExpression(Template ownerTemplate, string expressionText)
{
if (string.IsNullOrEmpty(expressionText)) return new ConstantExpression(expressionText);
if (expressionText.StartsWith("$") && !"$=".Equals(expressionText)) //"$="认为是字符串常量
{
expressionText = expressionText.Remove(0, 1);
if (expressionText.StartsWith("$"))
{
//$$字符开头.则认为是常量表达式
return new ConstantExpression(expressionText);
}
else
{
//变量表达式
return CreateVariableExpression(ownerTemplate, expressionText, false);
}
}
else
{
//常量表达式
return Utility.IsInteger(expressionText) ?
new ConstantExpression(Utility.ConverToInt32(expressionText))
: new ConstantExpression(expressionText);
}
}
示例2: Tag
/// <summary>
/// 标签元素
/// </summary>
/// <param name="ownerTemplate">宿主模板</param>
protected Tag(Template ownerTemplate) : base(ownerTemplate)
{
this.InnerElements = new ElementCollection<Element>();
this.Attributes = new AttributeCollection(this);
//注册添加属性时触发事件.用于设置自身的某些属性值
this.Attributes.Adding += OnAddingAttribute;
}
示例3: PreRender
/// <summary>
/// 解析某个模板块的数据
/// </summary>
/// <param name="template"></param>
public void PreRender(Template template)
{
//根据模板块里定义的type属性条件取得新闻数据
List<News> newsData = NewsDbProvider.GetNewsData(template.Attributes.GetValue("type"));
//设置变量newsdata的值
template.Variables.SetValue("newsdata", newsData);
//取得模板块下Id为newslist的标签(也即是在cnblogs_newsdata.html文件中定义的foreach标签)
Tag tag = template.GetChildTagById("newslist");
if (tag is ForEachTag)
{
//如果标签为foreach标签则设置其BeforeRender事件用于设置变量表达式{$:#.news.url}的值
tag.BeforeRender += (sender, e) =>
{
ForEachTag t = (ForEachTag)sender;
//取得当前项的值(因为foreach标签的数据源是List<News>集合,所以当前项的值类型为News实体)
News news = (News)t.Item.Value;
//设置当前项的变量表达式的值.也即是"{$:#.news.url}"变量表达式
t.Item.SetExpValue("url", NewsDbProvider.GetNewsUrl(news));
//当新闻不可见时.你可以取消本次输出
if (!news.Visible) e.Cancel = true;
};
}
}
示例4: Clone
/// <summary>
/// 克隆当前元素到新的宿主模板
/// </summary>
/// <param name="ownerTemplate"></param>
/// <returns></returns>
internal override Element Clone(Template ownerTemplate)
{
IncludeTag tag = new IncludeTag(ownerTemplate);
this.CopyTo(tag);
tag.File = this.File;
tag.Charset = this.Charset;
return tag;
}
示例5: VariableTag
/// <summary>
/// 带变量字段的初始化
/// </summary>
/// <param name="ownerTemplate"></param>
/// <param name="varExp"></param>
internal VariableTag(Template ownerTemplate, VariableExpression varExp)
: base(ownerTemplate)
{
//注册添加属性时触发事件.用于设置自身的某些属性值
this.Attributes = new AttributeCollection(this);
this.Attributes.Adding += OnAddingAttribute;
this.VarExpression = varExp;
this.CallFunctions = new List<IExpression>();
}
示例6: Template
/// <summary>
///
/// </summary>
/// <param name="ownerTemplate"></param>
internal Template(Template ownerTemplate)
: base(ownerTemplate)
{
this.Visible = true;
this.Charset = (ownerTemplate == null ? Encoding.Default : ownerTemplate.Charset);
this.Variables = new VariableCollection();
this.ChildTemplates = new ElementCollection<Template>();
this.fileDependencies = new List<string>();
this.UserDefinedFunctions = new UserDefinedFunctionCollection();
this.TagContainer = this;
}
示例7: ProcessBeginTag
/// <summary>
/// 开始解析标签数据
/// </summary>
/// <param name="ownerTemplate">宿主模板</param>
/// <param name="container">标签的容器</param>
/// <param name="tagStack">标签堆栈</param>
/// <param name="text"></param>
/// <param name="match"></param>
/// <param name="isClosedTag">是否闭合标签</param>
/// <returns>如果需要继续处理EndTag则返回true.否则请返回false</returns>
internal override bool ProcessBeginTag(Template ownerTemplate, Tag container, Stack<Tag> tagStack, string text, ref Match match, bool isClosedTag)
{
//判断标签的容器是否为ForEach标签
if (!(container is ForEachTag)) throw new ParserException(string.Format("未找到和{0}标签对应的{1}标签", this.TagName, this.EndTagName));
ForEachTag foreachTag = (ForEachTag)container;
if (foreachTag.Else != null) throw new ParserException(string.Format("{0}标签不能定义多个{1}标签", this.EndTagName, this.TagName));
foreachTag.Else = this;
return true;
}
示例8: CreateTag
/// <summary>
/// 构建标签元素
/// </summary>
/// <param name="ownerTemplate">宿主模板</param>
/// <param name="match"></param>
/// <param name="isClosedTag">是否是自闭合标签</param>
/// <returns></returns>
internal static Tag CreateTag(Template ownerTemplate, Match match,out bool isClosedTag)
{
string tagName = match.Groups["tagname"].Value;
isClosedTag = match.Groups["closed"].Success;
Tag tag = TagFactory.FromTagName(ownerTemplate, tagName);
if (tag == null) throw new ParserException(string.Format("不能识别的元素标签\"{0}\"", tagName));
ParseElementAttributes(tag, match);
return tag;
}
示例9: FromTagName
/// <summary>
/// 根据标签名建立标签实例
/// </summary>
/// <param name="ownerTemplate"></param>
/// <param name="tagName"></param>
/// <returns></returns>
internal static Tag FromTagName(Template ownerTemplate, string tagName)
{
if (!string.IsNullOrEmpty(tagName))
{
switch (tagName.ToLower())
{
case "for":
return new ForTag(ownerTemplate);
case "foreach":
return new ForEachTag(ownerTemplate);
case "foreachelse":
return new ForEachElseTag(ownerTemplate);
case "if":
return new IfTag(ownerTemplate);
case "elseif":
return new IfConditionTag(ownerTemplate);
case "else":
return new ElseTag(ownerTemplate);
case "template":
return new Template(ownerTemplate);
case "include":
return new IncludeTag(ownerTemplate);
case "expression":
return new ExpressionTag(ownerTemplate);
case "function":
return new FunctionTag(ownerTemplate);
case "property":
return new PropertyTag(ownerTemplate);
case "serverdata":
return new ServerDataTag(ownerTemplate);
case "set":
return new SetTag(ownerTemplate);
case "import":
return new ImportTag(ownerTemplate);
case "output":
return new OutputTag(ownerTemplate);
case "panel":
return new PanelTag(ownerTemplate);
case "datareader":
if (ownerTemplate.OwnerDocument.DocumentConfig != null
&& ownerTemplate.OwnerDocument.DocumentConfig.TagOpenMode == TagOpenMode.Full)
{
return new DataReaderTag(ownerTemplate);
}
else
{
return null;
}
}
}
return null;
}
示例10: ProcessBeginTag
/// <summary>
/// 开始解析标签数据
/// </summary>
/// <param name="ownerTemplate">宿主模板</param>
/// <param name="container">标签的容器</param>
/// <param name="tagStack">标签堆栈</param>
/// <param name="text"></param>
/// <param name="match"></param>
/// <param name="isClosedTag">是否闭合标签</param>
/// <returns>如果需要继续处理EndTag则返回true.否则请返回false</returns>
internal override bool ProcessBeginTag(Template ownerTemplate, Tag container, Stack<Tag> tagStack, string text, ref Match match, bool isClosedTag)
{
container.AppendChild(this);
if (!string.IsNullOrEmpty(this.File))
{
//修正文件地址
this.File = Utility.ResolveFilePath(this.Parent, this.File);
if (System.IO.File.Exists(this.File))
{
//增加到依赖文件列表
this.OwnerTemplate.AddFileDependency(this.File);
//解析数据
new TemplateDocument(ownerTemplate, this, System.IO.File.ReadAllText(this.File, this.Charset), ownerTemplate.OwnerDocument.DocumentConfig);
}
}
return !isClosedTag;
}
示例11: Clone
/// <summary>
/// 克隆当前元素到新的宿主模板
/// </summary>
/// <param name="ownerTemplate"></param>
/// <returns></returns>
internal override Element Clone(Template ownerTemplate)
{
ForTag tag = new ForTag(ownerTemplate);
base.CopyTo(tag);
tag.Index = this.Index == null ? null : this.Index.Clone(ownerTemplate);
return tag;
}
示例12: Clone
/// <summary>
/// 克隆当前元素到新的宿主模板
/// </summary>
/// <param name="ownerTemplate"></param>
/// <returns></returns>
internal override Element Clone(Template ownerTemplate)
{
SetTag tag = new SetTag(ownerTemplate);
this.CopyTo(tag);
tag.Variable = this.Variable == null ? null : this.Variable.Clone(ownerTemplate);
tag.Output = this.Output;
foreach (IExpression exp in this.Values)
{
tag.Values.Add((IExpression)(exp.Clone(ownerTemplate)));
}
return tag;
}
示例13: ProcessBeginTag
/// <summary>
/// 开始解析标签数据
/// </summary>
/// <param name="ownerTemplate">宿主模板</param>
/// <param name="container">标签的容器</param>
/// <param name="tagStack">标签堆栈</param>
/// <param name="text"></param>
/// <param name="match"></param>
/// <param name="isClosedTag">是否闭合标签</param>
/// <returns>如果需要继续处理EndTag则返回true.否则请返回false</returns>
internal override bool ProcessBeginTag(Template ownerTemplate, Tag container, Stack<Tag> tagStack, string text, ref Match match, bool isClosedTag)
{
if (this.Variable == null && !this.Output) throw new ParserException(string.Format("{0}标签中如果未定义Output属性为true则必须定义var属性", this.TagName));
if (this.Values.Count < 1) throw new ParserException(string.Format("{0}标签中缺少value属性", this.TagName));
if (this.Values.Count > 1 && this.Format == null) throw new ParserException(string.Format("{0}标签如果已定义多个value属性,则也必须定义format属性", this.TagName));
return base.ProcessBeginTag(ownerTemplate, container, tagStack, text, ref match, isClosedTag);
}
示例14: PreRenderTemplateByAttributeMethod
/// <summary>
/// 使用特性方法预解析模板数据
/// </summary>
/// <param name="renderInstance"></param>
/// <param name="renderMethod"></param>
/// <param name="template"></param>
internal static void PreRenderTemplateByAttributeMethod(string renderInstance, string renderMethod, Template template)
{
object render = GetRenderInstance(renderInstance);
if (render != null)
{
//取得特性方法
MethodInfo method = null;
MethodInfo[] methods = render.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
foreach (MethodInfo m in methods)
{
TemplateRenderMethodAttribute att = System.Attribute.GetCustomAttribute(m, typeof(TemplateRenderMethodAttribute)) as TemplateRenderMethodAttribute;
if (att != null)
{
if (renderMethod.Equals(m.Name, StringComparison.InvariantCultureIgnoreCase))
{
method = m;
break;
}
}
}
if (method != null)
{
ParameterInfo[] pars = method.GetParameters();
if (pars.Length == 1 && pars[0].ParameterType == typeof(Template))
{
method.Invoke(method.IsStatic ? null : render, new object[] { template });
}
}
}
}
示例15: GetVariableOrAddNew
/// <summary>
/// 从模板中获取某个变量.如果不存在此变量则添加新的变量
/// </summary>
/// <param name="ownerTemplate"></param>
/// <param name="varName"></param>
/// <returns></returns>
internal static Variable GetVariableOrAddNew(Template ownerTemplate, string varName)
{
Variable var = ownerTemplate.Variables[varName];
if (var == null)
{
var = new Variable(ownerTemplate, varName);
ownerTemplate.Variables.Add(var);
}
return var;
}