如何在Kentico宏中为邮件小部件设置多条件Where子句?
Kentico宏多条件查询正确写法
原单条件查询可正常运行:
style="background-color: {% CMSContext.Current.GlobalObjects.CustomTables["Keyton.EmailTemplateUIElements"].Items.Where("StyleKey = 'theme_bg_1'").FirstItem.StyleValue #%};"
尝试多条件查询时,错误地在Where条件字符串内嵌套宏标签导致语法错误,以下是两种正确写法:
写法1:直接拼接属性值
style="background-color: {% CMSContext.Current.GlobalObjects.CustomTables["Keyton.EmailTemplateUIElements"].Items.Where("StyleKey = 'theme_bg_1' and Theme = '" + Theme + "'").FirstItem.StyleValue #%};"
写法2:用字符串格式化提升可读性
style="background-color: {% var condition = string.Format("StyleKey = 'theme_bg_1' and Theme = '{0}'", Theme); CMSContext.Current.GlobalObjects.CustomTables["Keyton.EmailTemplateUIElements"].Items.Where(condition).FirstItem.StyleValue #%};"
关键说明
- 外层已处于宏解析上下文,无需在
Where的条件字符串内再次嵌套{% ... %}标签,直接引用小部件属性Theme即可。 - 必须用单引号包裹
Theme的值,确保它作为字符串参数传入查询条件。
内容的提问来源于stack exchange,提问作者chamara
相关产品推荐
相关产品推荐

