【问题标题】:Subtag context in DotLiquid (with forms)DotLiquid 中的子标签上下文(带表单)
【发布时间】:2017-12-24 17:34:12
【问题描述】:

我希望能够在子标签内访问传递给 DotLiquid 中的表单标签的对象。像这样的:

{% form '/action' requestObject %}
    {% textinput Name %}
    <button type="submit">Create Request</button>
{% endform %}

textinput 标签在requestObject 上查找name 字段,然后将值放入文本输入字段。我的 Liquidese 相当生疏,所以如果我把这一切都弄错了,我很乐意连贯地尖叫着说我是个傻瓜,我需要做些什么才能变得更好。

【问题讨论】:

    标签: c# .net-core liquid dotliquid


    【解决方案1】:

    这很容易在标签渲染器中使用ContextStack 完成:

    public class Form : Block
    {
        // public override void Initialize...
        public override void Render(Context context, TextWriter writer)
        {
            context.Stack(() =>
            {
                context["form_obj"] = new FormObject();
                result.Write("<form>");
                base.Render(context, result);
                result.Write("</form>");
            }
        }
    }
    

    在运行传递给它的操作之前,Stack 推送一个新的 Hash 变量堆栈(查找链中未设置的变量),然后在最后弹出它。非常适合变量的本地化范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      相关资源
      最近更新 更多