Forum

Thread tagged as: Problem

Variable Not being passed into template

Hello,

I'm trying to pass a variable id named sizes into an image template for responsive images. I'm using perch_content_create, for example:

    PerchSystem::set_var('sizes', '100vw');

    perch_content_create('Image', array(
    'template' => 'image_responsive.html',
    'multiple' => true,
    'edit-mode' => 'listdetail',
    'sort' => 'date',
    'sort-order' => 'DESC',
));

Then the template has a place holder:

<img src="<perch:content type="image" id="image" label="Image" help="Upload or select an image" />" alt="<perch:content type="text" id="alt" label="Description" required="true" help="Description of this image" title="true" />"
sizes="<perch:content id="sizes"/>"
srcset="<perch:content type="image" id="image" label="Image" width="200" /> 200w,
        <perch:content type="image" id="image" label="Image" width="400" /> 400w,
        <perch:content type="image" id="image" label="Image" width="800" /> 800w,
        <perch:content type="image" id="image" label="Image" width="1200" /> 1200w">

The variable isn't being passed through. I republish the pages and it still isn't there?

What am I doing wrong here?

Tony Astley

Tony Astley 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to call PerchSystem::set_var('sizes', '100vw'); when the region is displayed rather than created.

Hello Drew,

Could you explain what you mean by that please, I've tried the following and that didn't work either:

        PerchSystem::set_var('sizes', '100vw');
        perch_content('Image'); 
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll need to do this:

PerchSystem::set_var('sizes', '100vw'); 
perch_content_custom('Image', []); 

Because the HTML for perch_content() is precompiled at edit time, so changes you make in the page beforehand won't have any impact.

Fantastic, works perfectly. Out of interest what can we put in the square brackets?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

The documentation for perch_content_custom is here, which lists all of the options https://docs.grabaperch.com/functions/content/perch-content-custom/

Thanks Drew, I couldn't see that syntax is all.

Cheers for you help.