如何在UGC条件语句中写出大于条件

时间:2012-08-20 12:43:29

标签: tridion tridion-2011 ugc

我在我的代码中使用ugc条件语句,等于条件工作正常,但如何使用其他条件运算符如“>” “<”和“不等于”。

<%
HttpContext.Current.Items["CommentCount"] = 0;
%>

<ugc:Choose runat="server">
  <ugc:When test="ugcItemStats.numberOfComments > CommentCount" runat="server">
         HTML1
  </ugc:When>
  <ugc:Otherwise runat="server">
         HTML2
  </ugc:Otherwise>
</ugc:Choose>

应该使用哪个运算符,如果numberofComments大于0,我尝试这样,并尝试“notequals”而不是“&gt;”但它不起作用。

请建议

4 个答案:

答案 0 :(得分:4)

Tridion ug:如果您想使用其他运算符,何时只能使用“equal”和“==”,那么您必须为此创建另一个客户控件。

我已创建并希望它能与“==,&gt; =”,&lt; =,&gt;,&lt;,!=“运算符一起使用。

它在我的项目中工作。

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

namespace Tridion.ContentDelivery.UGC.Web.UI
{
    [DefaultProperty("Test"), ToolboxData("<{0}:WhenCond runat=server></{0}:WhenCond>"), ParseChildren(ChildrenAsProperties = false)]
    public class WhenCond : BaseUGCServerControl
    {
        private string test;
        private static Regex pattern = new Regex(@"\.");
        protected virtual bool Condition()
        {
            if (this.test == null)
            {
                return false;
            }
            string[] sep = new string[] { "==", "<", ">", "<=", ">=" ,"!="};
            string[] testArray = test.Split(sep, StringSplitOptions.None);
            if (testArray.Length == 2)
            {
                object value1 = EvaluateVariable(testArray[0].Trim(), HttpContext.Current);
                object value2 = EvaluateVariable(testArray[1].Trim(), HttpContext.Current);
                if (value1 != null && value2 != null)
                {
                    if (isNumeric(value1.ToString(), NumberStyles.Number) && isNumeric(value2.ToString(), NumberStyles.Number))
                    {
                        return NumericCondition(double.Parse(value1.ToString()), double.Parse(value2.ToString()), GetSepartor());
                    }
                    else
                    {
                        return AlphaNumericCondition(value1.ToString(), value2.ToString(), GetSepartor());
                    }
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public static object EvaluateVariable(string varProperty, HttpContext usedContext)
        {
            if (!string.IsNullOrEmpty(varProperty))
            {
                string[] strArray = pattern.Split(varProperty);
                if (!string.IsNullOrEmpty(strArray[0]))
                {
                    object obj2 = usedContext.Items[strArray[0]];
                    if (obj2 != null)
                    {
                        object obj3 = obj2;
                        for (int i = 1; i < strArray.Length; i++)
                        {
                            if (obj3 != null)
                            {
                                string str = strArray[i];
                                if (!string.IsNullOrEmpty(str))
                                {
                                    string str2 = str.Substring(0, 1);
                                    string str3 = str.Substring(1);
                                    string name = str2.ToUpper() + str3;
                                    PropertyInfo property = obj3.GetType().GetProperty(name);
                                    if (property != null)
                                    {
                                        obj3 = property.GetValue(obj3, null);
                                    }
                                }
                            }
                        }
                        return obj3;
                    }
                }
            }
            return null;
        }

        public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
        {
            Double result;
            return Double.TryParse(val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        private string GetSepartor()
        {
            string sept = string.Empty;
            sept = this.test.Contains("==") ? "==" : string.Empty;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains(">") ? ">" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ? (this.test.Contains("<") ? "<" : string.Empty) : sept;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains(">=") ? ">=" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ?(this.test.Contains("<=") ? "<=" : string.Empty):sept;
            sept = string.IsNullOrEmpty(sept) ? (this.test.Contains("!=") ? "!=" : string.Empty) : sept;
            return sept;
        }

        private bool NumericCondition(double value1, double value2, string sept)
        {
            bool returnFlag = false;
            switch (sept)
            {
                case "==":
                    returnFlag = (value1 == value2);
                    break;
                case ">":
                    returnFlag = (value1 > value2);
                    break;
                case "<":
                    returnFlag = (value1 < value2);
                    break;
                case ">=":
                    returnFlag = (value1 >= value2);
                    break;
                case "<=":
                    returnFlag = (value1 <= value2);
                    break;
                case "!=":
                    returnFlag = (value1 != value2);
                    break;
            }
            return returnFlag;
        }

        private bool AlphaNumericCondition(string value1, string value2, string sept)
        {
            bool returnFlag = false;
            switch (sept)
            {
                case "==":
                    returnFlag = (value1.CompareTo(value2) == 0);
                    break;
                case "!=":
                    returnFlag = (!value1.Equals(value2));
                    break;
                case ">":
                    returnFlag = (value1.CompareTo(value2) > 0);
                    break;
                case "<":
                    returnFlag = (value1.CompareTo(value2) < 0);
                    break;
            }
            return returnFlag;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if ((HttpContext.Current != null) && (HttpContext.Current.Application != null))
            {
                Control parent = this.Parent;
                if (!(parent is Choose))
                {
                    throw new InvalidOperationException("WhenCond control must have a Tridion Web UI Choose server control as parent!!!");
                }
                Choose choose = (Choose)parent;
                if (!choose.AlreadyMatchedCondition() && this.Condition())
                {
                    choose.MatchedCondition();
                    this.RenderChildren(writer);
                }
            }
        }

        [Category("Appearance"), DefaultValue(""), Bindable(true)]
        public string Test
        {
            get
            {
                return this.test;
            }
            set
            {
                this.test = value;
            }
        }
    }
}

在aspx页面中实现

<%@ Register assembly="Tridion.Custom.Web.UI" namespace="Tridion.ContentDelivery.UGC.Web.UI" tagprefix="cc1" %>


<ugc:Choose runat="server">
  <cc1:WhenCond test="ugcItemStats.numberOfComments > CommentCount" runat="server">
         HTML1
  </cc1:WhenCond>
  <ugc:Otherwise runat="server">
         HTML2
  </ugc:Otherwise>
</ugc:Choose>

如果您遇到任何问题,请告诉我。

答案 1 :(得分:2)

据我所知,When条件仅支持==equals两个条件。因此,您可能必须使用这些来解决您的测试条件(两者都相同)。

有人可以验证或确认上述情况属实吗?

答案 2 :(得分:1)

您是否尝试过使用&amp; gt;?这通常可以解决类似问题,例如:的XPath。

答案 3 :(得分:1)

抱歉,标记目前仅支持“==”或“等于”。