sqwk
sqwk

Reputation: 2699

Passing ViewBag Variable to Component

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?

Upvotes: 0

Views: 541

Answers (2)

sqwk
sqwk

Reputation: 2699

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 %}

Upvotes: 1

Joseph Oppegaard
Joseph Oppegaard

Reputation: 621

In order to pass the variable to the component, you have to do it in the twig section where you use the component. Here is an example:

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

I just tested this with one of my test components and it worked as expected.

Upvotes: 1

Related Questions