Forum

Thread tagged as: Problem

perch_template() outputting HTML onto the page.

I'm having an issue where perch_template() is outputting encoded HTML onto my page.

I'm pulling in some collection items

$contestants =  perch_collection('Gladiators', array(
                    'skip-template' => true,
                    'sort' => 'score',
                    'sort-order' => 'ASC',
                    'count' => 5
                ));

Shuffling the array and and taking the top two...

//Randomise them
shuffle($contestants);

//Pit the top two against each otehr
$con1 = $contestants[0];
$con1['index'] = 1;
$con2 = $contestants[1];
$con2['index'] = 2;

and then using perch_template() to output the html...

<?php perch_template('content/collections/gladiator.html', $con1); ?>

and

<?php perch_template('content/collections/gladiator.html', $con2); ?>

This results in <p> tags being output around the markdown fields. Specifically, the testament field has <p> tags around it.

I'm using 2.8.21. Template and Diagnostics are below.

Collection template (gladiator.html)

    <div class="contest-box">
        <div class="avatar-wrapper">
            <img src="images/avatar1.png" alt="" />
        </div>
        <h2><perch:content id="fname" type="smarttext" label="First Name" required="true" title="true" /> <perch:content id="lname" type="smarttext" label="Last Name" required="true" title="true" /></h2>
        <p><strong>Position</strong><br><perch:related id="position" collection="Positions" label="Position" help="Please select only one"><perch:content id="position" /></perch:related>, <perch:content id="positon_location" type="smarttext" label="Position location" required="true" help="e.g. Bristol South" /></p>
        <p><strong>Organisation</strong><br><perch:content id="organisation" type="smarttext" label="Organisation" required="true" help="e.g. Bristol City Council" /></p>
        <p><strong>Service</strong><br><perch:content id="service" type="smarttext" label="Service" required="true" /></p>
        <div class="row small-buttons">
            <div class="col-md-4">
                <a class="btn btn-block btn-blue" href="#" role="button" data-popup="my-record-<perch:content id="index" type="hidden" />">MY RECORD</a>
            </div>
            <div class="col-md-4">
                <a class="btn btn-block btn-blue iframe-popup" href="<perch:content id="video" type="text" label="Video URL" required="true" help="e.g. https://www.youtube.com/watch?v=0O2aH4XLbto" />" role="button">MY VIDEO</a>
            </div>
            <div class="col-md-4">
                <a class="btn btn-block btn-blue" href="#" role="button" data-popup="my-testimony-<perch:content id="index" type="hidden" />">MY TESTIMONY</a>
            </div>
        </div>
        <div class="inline-popup my-record-<perch:content id="index" type="hidden" />" data-btn="my-record-<perch:content id="index" type="hidden" />">
            <h3><perch:content id="fname" />’s record</h3>
            <perch:content id="record" type="textarea" label="Record" markdown="true" editor="markitup" required="true"  />
            <a href="#" class="btn-close">Close</a>
        </div>
        <div class="inline-popup my-testimony-<perch:content id="index" type="hidden" />" data-btn="my-testimony-<perch:content id="index" type="hidden" />">
            <h3><perch:content id="fname" />’s testament</h3>
            <perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" encode="false"  />
            <a href="#" class="btn-close">Close</a>
        </div>
    </div>

Diagnostics

Perch Runway: 2.8.21, PHP: 5.6.10, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (2.8.21), assets (2.8.21), categories (2.8.21), perch_forms (1.8.3), collection_1 (2.8.21), collection_2 (2.8.21), perch_members (1.4), austerity_arena (1)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_forms', 'perch_members', 'austerity_arena' );
PERCH_LOGINPATH: /perch
PERCH_PATH: [...]/httpdocs/perch
PERCH_CORE: [...]/httpdocs/perch/core
PERCH_RESFILEPATH: [...]/httpdocs/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
Resource folder writeable: Yes
HTTP_HOST: austarena:8888
DOCUMENT_ROOT: [...]/httpdocs
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Caleb Evans

Caleb Evans 1 points

  • 5 years ago

Caleb, Just in case you didn't know, there is sort="RAND" and count="2"

But I didn't get a chance to really read your post so I may have missed something.

RK

Oh... Also, you need encode="false" on the tag in your template

Thanks for the reply Robert.

I already have encode=false on the tag in question.

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" encode="false"  />

Does that look correct?

As for the ordering, I'm using a score field to pull out the 5 lowest scoring entries ad then getting two random ones from that subset. I could not think of a way to do this in one hit with Perch.

Is that in the template you used to create the data or the one you're using in the perch template you're running the content through for output. Sorry, now I'm driving down the road

Both. There is only one template at the moment.

OK, at my next stop I'll pull out the iPad and see if I can come up with something

Caleb, any text entered in the testament field will have <p> tags because markitup will add this. Are the tags being out put to the human view, or just in the html of the page?

In other words in html is it <p> or lt; p gt;

Sorry, my iPad is really making this hard because it keeps auto correcting.

Hi Robert

Yes, understand that.

The source HTML shows the tags encoded...

&lt;p&gt;Curabitur blandit tempus [...] porta felis euismod semper.&lt;/p&gt;

Do you think that perch_template() won't do what I am asking of it?

I saw what I thought was a similar issue at this post. Might be a red herring.

I think it should work, but I can't see where it's getting encoded, plus, now that I am on the road for the day, I can't test your code on my dev server so I won't be much help till this eve if it's still an issue. :(

Thanks Robert.

I've had to move onto something more urgent for the time being because I can't figure it out. If you get a chance to look later it would be very much appreciated.

I figured it out.... and this is going to take Drew's help for sure...

In your template...

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" encode="false"  />

When rendering with the normal...

perch_collection('Gladiators', array(
    'sort' => 'score',
    'sort-order' => 'ASC',
    'count' => 5
));

it renders correctly, it's when you capture the return into $contestants and send it back to be templated with perch_template() that the second <p> tags are applied, but the original <p> tags are being encoded and output in the rendered code. So there is a problem (sorry Drew) with how perch_template() is dealing with the textarea.

Here is a work around that works beautiful...

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" suppress="true" />
<perch:content id="testament" encode="false" hidden="true" />

The first "testament" field is the one you see in edit, but the second "testament" field is the one rendered in the html.

Somehow, the textarea is being rendered with a second set of <p> tags which ONLY happens when when skip-template is used, then perch_template() is used to render later.

I have an entire working example of this should any template files be needed to replicate the problem.

EDIT: During "edit" the encode="false" is ignored, it's only during output (rendering) that it gets applied, but because "skip-template" is used, the encoding never happens. Then when the template is used again with perch_template() somehow the encode="false" is being ignored again and the textarea is output with one set of tags encoded, and the other not.

Man am I getting a headache... :(

@Caleb, a second workaround is to have a duplicate template which you use with perch_content() and use <perch:content id="testament" encode="false" /> in place of the full <perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" /> tag in the primary template. But, it's a pain trying to maintain (2) templates like this.

RK

Drew McLellan

Drew McLellan 2638 points
Perch Support

This is very hard to follow, but I think you're going to need two templates. Going into the first, the content is Markdown, but going into the second it's HTML. They need to be treated differently.

Certainly the encoding through perch_template() is working as designed - I've double checked our unit tests.

Sorry Drew, I am redacting my thought of a problem with perch_template() and I may edit above to remove some of the confusion.

Now, looking at perch_template() I am realizing it does not see the markup as "markup" like it would when the data is in "edit" mode in the original template (raw and processed). So, when HTML is passed to perch_template() if you have more then "id" attribute on the textarea it will treat this HTML as the "raw" input and apply additional HTML around that input then output "processed".

So the final thought on this is, perch_template() really only parses the array of fields into the template, it does not render the data it the way the normal "edit" mode would.

Thanks for the perseverance with this.

If I now understand correctly I'll need separate templates if outputting textarea fields using perch_template().

So, for the edit form my templet field will look pretty standard.

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true"  />

But when outputting using perch_template() I'll need to ensure encoding is not carried out again.

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" encode="false"  />

Is that correct?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What content are you passing in? Is it HTML?

<perch:content id="testament" type="textarea" encode="false"  />

This is what it currently looks like in my template

<perch:content id="testament" type="textarea" label="Testament" markdown="true" editor="markitup" required="true" encode="false"  />

It is being retrieved using

$contestants =  perch_collection('Gladiators', array(
                    'skip-template' => true,
                    'sort' => 'score',
                    'sort-order' => 'ASC',
                    'count' => 5
                ));

and then output to the page using something like

<?php perch_template('content/collections/gladiator.html', $contestants[0]); ?>

Does that answer your question?

I'm not 100% clear on where the double encode is taking place.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It doesn't really. Do you have an example?

I've sent you a DM on Slack as I cannot make the URL public.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I was more interested in the data than your page. This is really beyond the scope of support, however.