Ad

Passing ViewBag Variable To Component

- 1 answer

When customising the markup of a gallery component (Pollozen SimpleGallery), passing in a slug manually works fine. (The gallery component is used inside a layout).

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

When trying to assign a variable that is set by a static page (Rainlab Pages) nothing happens

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

{{page.galleryId}} is displayed fine as "test" inside the actual layout. How should I assign viewBag variables to components to make this work?

Ad

Answer

I ended up having to create a partial to nest the component. Not sure why passing the variable directly does not work, but I suspect that static page variables are evaluated after the components are initialised.

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

[viewBag]

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

==

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

And inside the layout:

{% partial 'gallery' id=page.galleryId %}
Ad
source: stackoverflow.com
Ad