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, 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.
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...
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...
When rendering with the normal...
it renders correctly, it's when you capture the return into
$contestants
and send it back to be templated withperch_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 howperch_template()
is dealing with the textarea.Here is a work around that works beautiful...
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
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 toperch_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.
But when outputting using
perch_template()
I'll need to ensure encoding is not carried out again.Is that correct?
What content are you passing in? Is it HTML?
This is what it currently looks like in my template
It is being retrieved using
and then output to the page using something like
Does that answer your question?
I'm not 100% clear on where the double encode is taking place.
It doesn't really. Do you have an example?
I've sent you a DM on Slack as I cannot make the URL public.
I was more interested in the data than your page. This is really beyond the scope of support, however.