private ITemplateDefinition AddDataField(Type dataType, PropertyInfo property, IDataFieldFormatter dataFormatter = null, string scopeName = null) { BodyActions.Add(r => { string formattedValue; var data = r.Data.Get(dataType, scopeName); if (data == null) { if (dataFormatter == null) { formattedValue = null; } else { formattedValue = dataFormatter.Format(property, null); } } else { var propertyValue = property.GetValue(data, null); if (dataFormatter == null) { formattedValue = propertyValue == null ? null : propertyValue.ToString(); } else { formattedValue = dataFormatter.Format(property, propertyValue); } } if (!string.IsNullOrEmpty(formattedValue)) { r.Html.WriteText(formattedValue); } }); AddDependency(dataType, scopeName); return(this); }
public ITemplateDefinition SetElementAttribute(string attributeName, Type dataType, string propertyName, IDataFieldFormatter dataFormatter = null, string scopeName = null) { if (_element == null) { throw new TemplateBuilderException("You cannot set element attributes " + "here since there is no current element. Call AddElementOpen() before setting element attributes."); } _element.Attributes.Add(attributeName); _element.Attributes.Add(attributeName); var element = _element; var attributeIndex = _element.Attributes.Count - 1; var property = dataType.GetProperties().FirstOrDefault(p => string.Equals(p.Name, propertyName, StringComparison.OrdinalIgnoreCase)); if (property == null) { throw new TemplateBuilderException("Type " + dataType.DisplayName() + " does not have a public '" + propertyName + "' property"); } BodyActions.AddPrior(r => { var data = r.Data.Get(dataType, scopeName); var propertyValue = property.GetValue(data, null); var formattedValue = dataFormatter == null ? propertyValue.ToString() : dataFormatter.Format(property, propertyValue); element.Attributes[attributeIndex] = formattedValue; }); var dataConsumer = _template as IDataConsumer; if (dataConsumer != null) { dataConsumer.HasDependency(dataType, scopeName); } return(this); }