将ViewBag变量传递给组件

时间:2018-03-14 10:15:49

标签: octobercms

在自定义图库组件(Pollozen SimpleGallery)的标记时,手动传入slug可以正常工作。 (图库组件在布局中使用)。

[Gallery]
idGallery = 0
markup = "user"
slug = "test"

当尝试分配由静态页面设置的变量(Rainlab Pages)时,没有任何反应

[Gallery]
idGallery = 0
markup = "user"
slug = {{page.galleryId}}

{{page.galleryId}}显示为" test"在实际布局内。我应该如何将viewBag变量分配给组件以使其工作?

2 个答案:

答案 0 :(得分:1)

为了pass the variable to the component,您必须在使用该组件的树枝部分中进行此操作。这是一个例子:

[Gallery]
idGallery = 0
markup = "user"
==
{% component 'Gallery' slug=viewBag.slug %}

我刚用我的一个测试组件对它进行了测试,并按预期工作。

答案 1 :(得分:1)

我最终不得不创建一个部分来嵌套组件。不确定为什么直接传递变量不起作用,但我怀疑在初始化组件后会评估静态页面变量。

public void deleteWithValue(int searchValue) {
    DoublyLinkedListNode current = head;

    while (current != null) {

        // If true, then the current node is the one to remove
        if (current.data == searchValue) {  

            if (current == head) {
                head = head.next;
            }

            // Ensure the previous node maintains a link to the next node
            if (current.prev != null) {
                current.prev.next = current.next;
            }

            // Ensure the next node maintains a link to the previous node   
            if (current.next != null) {
                current.next.prev = current.prev;
            }

            break;
        }
        current = current.next;
    }
}

在布局中:

description = "Gallery (nested for static pages compatibility)"

[viewBag]

[Gallery]
idGallery = 0
markup = "user"
slug = "{{ id }}"

==

{% for image in gallery.images %}
    ...
{% endfor %}