Forum

Thread tagged as: Question, Addons, Events

Passing Variable into Blocks

Hi guys,

I've got some PHP code running to get some events:

$upcoming_events = perch_events_custom([
            'filter'=>'eventDateTime',
            'match' =>'gte',
            'value' =>date("Y-m-d"),
            'count' =>6,
            'template'=>'/default/upcoming-event.html',
            'skip-template'=>true
        ]); 
        PerchSystem::set_var('upcoming_events',$upcoming_events);

and I'm trying to call it into a Perch Block like this:

    <perch:block type="upcoming-events" label="Upcoming Events">
        <perch:help>
            <p>This section will display the next six upcoming events, including events today.</p>
        </perch:help>
        <perch:content id="upcoming_events" />
    </perch:block>

But nothing's coming up on the front end (apart from the print_r command, obviously).

Also, the help doesn't show up inside the block, either. When adding the block, the dashboard view shows an empty, unlabelled text input, presumably the <perch:content id="upcoming_events" /> field.

Any ideas? Is this possible in Perch?

Harry Ray

Harry Ray 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

  1. You need to pass the variables in before the content is templated
  2. You need to use a perch:events tag in an events template.
  3. You'll need to scope in parent variables for the block to be able to access your variable, I believe.

Hi Drew,

Thanks for the help here - almost there, I think!

So, the events_custom( ) call is now working, and the variable is being set and passed through into the Block (inside the Page Content region):

<?php
        $upcoming_events = perch_events_custom([
            'filter'=>'eventDateTime',
            'match' =>'gte',
            'value' =>date("Y-m-d"),
            'count' =>6,
            'template'=>'/default/upcoming-event.html'
        ],true);    
        PerchSystem::set_var('upcoming_events',$upcoming_events);
        perch_content_custom('Page Content');
    ?>

Trouble is that I need to use the /default/upcoming-event.html template to render this data (which is why it's in the events_custom( ) call, but the variable actually just displays the code that the events_custom( ) call returns, rather than rendering it as HTML - any ideas?

Thanks!

EDIT - Sorry, solved! I've added encode="false" to the variable call in the Page Content region - thanks for the help on this, Drew!