Пример #1
0
		/// <summary>
		/// 从元标签库为指定的模板初始化子模板对象。
		/// </summary>
		/// <param name="name">指定模板名称。</param>
		/// <param name="elements">指定一个元标签库。</param>
		/// <param name="parent">指定父模板的实例。</param>
		public Template(string name, List<Element> elements, Template parent)
		{
			_Name = name;
			_Elements = elements;
			_Parent = parent;

			_InitTemplates();
		}
Пример #2
0
		/// <summary>
		/// 从元标签库初始化模板对象。
		/// </summary>
		/// <param name="name">指定模板名称。</param>
		/// <param name="elements">指定一个元标签库。</param>
		public Template(string name, List<Element> elements)
		{
			_Name = name;
			_Elements = elements;
			_Parent = null;

			_InitTemplates();
		}
Пример #3
0
		private ITemplateHandler _Handler;		// handler will be set as "this" object

		#endregion

		#region 构造函数

		/// <summary>
		/// 使用模板初始化模板管理器实例。
		/// </summary>
		/// <param name="template">指定一个模板实例。</param>
		public TemplateManager(Template template)
		{
			_MainTemplate = template;
			_CurrentTemplate = template;
			_SilentErrors = false;

			_Init();
		}
Пример #4
0
		private void _InitTemplates()
		{
			_Templates = new Dictionary<string, Template>(StringComparer.InvariantCultureIgnoreCase);

			foreach (Element elem in _Elements)
			{
				if (elem is Tag)
				{
					Tag tag = (Tag)elem;
					if (string.Compare(tag.Name, "template", true) == 0)
					{
						Expression ename = tag.AttributeValue("name");
						string tname;
						if (ename is StringLiteral)
							tname = ((StringLiteral)ename).Content;
						else
							tname = "?";

						Template template = new Template(tname, tag.InnerElements, this);
						_Templates[tname] = template;
					}
				}
			}
		}
Пример #5
0
		/// <summary>
		/// 处理模板。将处理结果保存到字符编写器中。
		/// </summary>
		/// <param name="writer">指定一个字符编写器。</param>
		public void Process(TextWriter writer)
		{
			_Writer = writer;
			_CurrentTemplate = _MainTemplate;

			if (_Handler != null)
			{
				SetValue("this", _Handler);
				_Handler.BeforeProcess(this);
			}

			this.ProcessElements(_MainTemplate.Elements);

			if (_Handler != null)
				_Handler.AfterProcess(this);
		}
Пример #6
0
		/// <summary>
		/// 添加一个模板实例。
		/// </summary>
		/// <param name="template">指定一个模板实例。</param>
		public void AddTemplate(Template template)
		{
			_MainTemplate.Templates.Add(template.Name, template);
		}
Пример #7
0
		/// <summary>
		/// 处理模板。
		/// </summary>
		/// <param name="name">指定模板名。</param>
		/// <param name="tag">指定一个标签实例。</param>
		protected void ProcessTemplate(string name, Tag tag)
		{
			if (_CustomTags != null && _CustomTags.ContainsKey(name))
			{
				ExecuteCustomTag(tag);
				return;
			}

			Template useTemplate = _CurrentTemplate.FindTemplate(name);
			if (useTemplate == null)
			{
				string msg = string.Format("模板未找到:'{0}'", name);
				throw new TemplateRuntimeException(msg, tag.Line, tag.Col);
			}

			// process inner elements and save content
			TextWriter saveWriter = _Writer;
			_Writer = new StringWriter();
			string content = string.Empty;

			try
			{
				this.ProcessElements(tag.InnerElements);

				content = _Writer.ToString();
			}
			finally
			{
				_Writer = saveWriter;
			}

			Template saveTemplate = _CurrentTemplate;
			_Variables = new VariableScope(_Variables);
			_Variables["innerText"] = content;

			try
			{
				foreach (TagAttribute attrib in tag.Attributes)
				{
					object val = this.EvalExpression(attrib.Expression);
					_Variables[attrib.Name] = val;
				}

				_CurrentTemplate = useTemplate;
				this.ProcessElements(_CurrentTemplate.Elements);
			}
			finally
			{
				_Variables = _Variables.Parent;
				_CurrentTemplate = saveTemplate;
			}
		}