/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output; var attrib = context.GetTalAttribute(ZptConstants.Tal.OmitTagAttribute); if(attrib != null) { if(attrib.Value.Length == 0) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else { // Normal handling by detecting an attribute and using its value var result = context.TalModel.Evaluate(attrib.Value, context); if(!result.CancelsAction && result.GetValueAsBoolean()) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else { output = new AttributeHandlingResult(new [] { context }, true); } } } else { output = new AttributeHandlingResult(new [] { context }, true); } return output; }
/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output; var attrib = context.GetTalAttribute(ZptConstants.Tal.OnErrorAttribute); if(attrib != null) { string mode; var expressionResult = this.GetAttributeResult(attrib, context, out mode); if(expressionResult.CancelsAction) { output = new AttributeHandlingResult(new [] { context }, true); } else if(expressionResult.Value == null) { context.Element.RemoveAllChildren(); output = new AttributeHandlingResult(new [] { context }, true); } else { context.Element.ReplaceChildrenWith(expressionResult.GetValue<string>(), mode == STRUCTURE_INDICATOR); output = new AttributeHandlingResult(new [] { context }, true); } ZptConstants.TraceSource.TraceInformation(Resources.LogMessageFormats.TalErrorHandled, context.Element.GetFullFilePathAndLocation(), nameof(OnErrorAttributeHandler), nameof(Handle)); } else { output = new AttributeHandlingResult(new [] { context }, true); } return output; }
/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output = new AttributeHandlingResult(new [] { context }, true); var attribute = context.GetTalAttribute(ZptConstants.Tal.ConditionAttribute); if(attribute != null) { ExpressionResult result; try { result = context.TalModel.Evaluate(attribute.Value, context); } catch(Exception ex) { string message = String.Format(ExceptionMessages.ExpressionEvaluationException, ZptConstants.Tal.Namespace, ZptConstants.Tal.ConditionAttribute, attribute.Value, context.Element.Name); throw new ModelEvaluationException(message, ex) { ExpressionText = attribute.Value, ElementName = context.Element.Name, }; } var removeElement = result.CancelsAction? false : !result.GetValueAsBoolean(); if(removeElement) { context.Element.Remove(); output = new AttributeHandlingResult(new RenderingContext[0], false); } } return output; }
/// <summary> /// Handle the related attribute types which exist upon the element exposed by the given context, if any. /// </summary> /// <returns>A response type providing information about the result of this operation.</returns> /// <param name="context">The rendering context, which exposes a ZPT element.</param> public AttributeHandlingResult Handle(IRenderingContext context) { if(context == null) { throw new ArgumentNullException(nameof(context)); } AttributeHandlingResult output; string attribName; var attrib = this.GetAttribute(context, out attribName); if(attrib != null) { string mode; var expressionResult = this.GetAttributeResult(attrib, context, out mode); if(expressionResult.CancelsAction && attribName == ZptConstants.Tal.ContentAttribute) { output = new AttributeHandlingResult(new [] { context }, true); } else if(expressionResult.CancelsAction && attribName == ZptConstants.Tal.ReplaceAttribute) { var children = context.Element.Omit(); output = new AttributeHandlingResult(new RenderingContext[0], false, children.Select(x => context.CreateSiblingContext(x)).ToArray()); } else if(expressionResult.Value == null) { if(attribName == ZptConstants.Tal.ContentAttribute) { context.Element.RemoveAllChildren(); output = new AttributeHandlingResult(new [] { context }, true); } else { context.Element.Remove(); output = new AttributeHandlingResult(new RenderingContext[0], false); } } else { if(attribName == ZptConstants.Tal.ContentAttribute) { var resultValue = expressionResult.GetValue<object>(); context.Element.ReplaceChildrenWith((resultValue?? String.Empty).ToString(), mode == STRUCTURE_INDICATOR); output = new AttributeHandlingResult(new [] { context }, true); } else { var resultValue = expressionResult.GetValue<object>(); var elements = context.Element.ReplaceWith((resultValue?? String.Empty).ToString(), mode == STRUCTURE_INDICATOR); output = new AttributeHandlingResult(elements.Select(x => context.CreateSiblingContext(x, true)).ToArray(), true); } } } else { output = new AttributeHandlingResult(new [] { context }, true); } return output; }