Forum

Thread tagged as: Question

Set variable in PHP and output in Perch Block - Perch Blog

Using Perch Blog I want to have a block in which there will be a select drop down. The HTML for this is generated with PHP.

However, I cannot seem to access the variable. I have a perch block in post.html like so:

            <perch:block type="select-block" label="Select an option">
                <div class="row select-row">
                    <div class="col-lg-12">
                        <div class="select-wrap">
                            <perch:template path="blog/custom/select.html" />
                        </div>
                    </div>
                </div>
            </perch:block>

I set the select_options_html var like so (just after including runtime.php):

PerchSystem::set_vars([
    'select_options_html' => $select_options_html,
]);

And call this variable in my template file (blog/custom/select.html):

<div class="module">
    <div class="select-option">
        <perch:blog id="select_options_html" />
    </div>
</div>

Yet this is not getting output. Where am I going wrong with this? Many thanks.

Michael Palmer

Michael Palmer 0 points

  • 2 months ago
Drew McLellan

Drew McLellan 2387 points
Perch Support

Looks like it should work. Do you see the value with <perch:showall /> ?

Yes I can see it there. I have managed to get a little further with it. When in adding or editing a new blog when I add the Perch Block (select-block) I do not see the select HTML at all. The following HTML is being output here instead:

<label for="perch__blocks_1_select_options_html" class=""></label>
<div class="form-entry">
    <input type="text" id="perch__blocks_1_select_options_html" name="perch__blocks_1_select_options_html" value="" class="text m input-simple">
</div>

And when viewing the actual post this is obviously output as a string of HTML code rather than rendering the element.

Drew McLellan

Drew McLellan 2387 points
Perch Support

You can stop it being encoded with encode="false" on the tag.

Excellent, thank you. That works for viewing the select on the blog post page. But from the admin side when editing or adding a blog post how do I show the select there? I want my admins to be able to make a selection from this when creating a post. And the selected value output in the post page.

Drew McLellan

Drew McLellan 2387 points
Perch Support

To do that you'd need to create a field type.

Wonderful, I will look into it. Thanks for your help.