Forum

Thread tagged as: Question, Problem

How do I pass a variable from a Template to a Layout and back?

I want to pass a filepath from a Template (which as I understand it, does not accept PHP) to a Layout so that I can process that file with ImageMagick and then send the new filepath back to my template. How do I do this?

This is what I tried:

publication.html (the Template)

<perch:layout path="pub.img" file='<perch:content id="PubDownloadLink" type="file" label="Upload Publication PDF File" />' />
<img src='<perch:content id="NewImg" />' />

pub.img.php (the Layout)

$pdf_file = perch_layout_var('file');
// process file
$new_file = '../img/' . $processed_file . time();
PerchSystem::set_var('NewImg', "$new_file");
Mike Harrison

Mike Harrison 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

<perch:layout path="pub.img" file="PubDownloadLink" />

BUT you probably don't want to be processing anything with ImageMagick at that point, as this will happen as the page loads.

That didn't work I'm afraid even echoing the variable displays nothing:

publication.html

<a href='<perch:content id="PubDownloadLink" type="file" label="Upload Publication PDF File" bucket="Publications"/>'>
  Download
</a>
<perch:layout path="pub.pdfimg" file='PubDownloadLink' />

pub.img.php

$pdf_file = perch_layout_var('file');
echo "<p>$pdf_file</p>";

No output.

Mike.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You've used single quotes, which aren't valid syntax for a Perch template tag.

Yep that's working now. Thanks Drew.

Any ideas how I would go about processing with Image Magick after the page loads?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to do the processing at edit time, ideally. Or at the least before the resource is needed,

I was hoping to process at edit time but didn't think you could do that with Perch? How would I go about running the layout code only in edit mode?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you rewind and outline what you're trying to accomplish?

I have a page on my client's website called 'Publications'. This page lists the publications in a table, with columns 'Image', 'Publication Title' and 'Download link'. The client can add publications by filling in the Title and uploading a PDF file. The image will be generated from the first page of the PDF using Image Magick. At the moment I believe the images are generated as the page loads instead of at the edit stage or after the page loads, which may slightly compromise page load speed. Previously you said "you need to do the processing at edit time" - how would I go about doing this?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I was going to say that you need to listen for the assets.file_uploaded event, but it looks like we don't have one. Only for images. That's a shame - I'll see if we can add it.

That would be great. Let me know if/when you do implement it. Thanks.