Forum

Thread tagged as: Question

Modify a template output

Hello,

I'm a new user, so I'm playing around I trying to understand the Perch workflow to start developing our first site using Perch. I'm doing little things and reading the documentation, and right now I'm stuck with something.

I need to create an activity calendar, a simple list of dates and a descriptions. So each date has a description about that activity. I created a new template at templates/content/calendar.html with this content:

<p><perch:content id="activity" title="true" type="text" label="Activity" help="Activity description" /></p>
<p><perch:content id="date" type="date" label="Date" help="Activity date" /></p>

Next, I added a new region to my home page:

perch_content('Calendar');

Later I checked the box "Share across all pages" on my page region, because I need to replicate the calendar along all my pages. Finally I added a few items to the calendar. The items are listed on my home page as expected. So, I copied the region to another page and the items are listed as well.

So, here is my problem, and I don't know how to achieve this: I need to manipulate the dates on the list. So, if the date is 7 Aug 2007, I need to only extract the day and month, and print: 7, Aug.

I tried with perch_content_custom and the each option to manipulate the items, and works, but, I don't wanna replicate that piece of code on all my pages where I need to list my calendar, is not clean. So, I'm looking for a simplest solution as just typing perch_content('Calendar') and automatically manipulate the output of the template.

Cheers.

Agustín Amenábar L.

Agustín Amenábar L. 0 points

  • 7 years ago

Ok, what I did finally is a mix between layouts and templates.

  • I modified my template, leaving only Perch fields:
<perch:content id="activity" title="true" type="text" label="Activity" help="Activity description" />
<perch:content id="date" type="date" label="Date" help="Activity date" />
  • I created a layout named calendar.php using the perch_content_custom function with skip-template and here is where I finally manage the calendar output:
<?php

$calendar = perch_content_custom('Calendar', array(
    'skip-template' => true
));

foreach($calendar as $activity) {
    $date = explode('-', $activity['date']);
    $month = jdmonthname($date[1], 0);
?>
    <h1><?=$month?></h1>
    <p><?=$activity['activity']?></p>
<?php
}
  • Finally on all my pages where I need the calendar, I included the layout:
perch_layout('calendar');

So I'm using the Perch templates for managing the fields and the layout for presenting the calendar.

I don't know if this is the right way, I will wait a answer from experienced people to get some feedback.

Agustín, you can use the "format" tag attribute, since you want to manipulate a date, the date field docs has some examples

Thanks for the tip Andrés!

I was writing:

But, if I need for example extract the month and year from a date field, and display the values in different HTML tags. I think the Perch template system it not help in those cases.

And I answer to my self according the docs we can re-use an ID of a field and use the content in different places on a template.

But my above example mixing templates and layouts, may be useful for other cases, I think, maybe if you need more flexibility than the templates gives us.

Drew McLellan

Drew McLellan 2638 points
Perch Support

So the way to do this in the template would be:

<perch:content id="date" type="date" label="Date" help="Activity date" format="M" />
<perch:content id="date" type="date" format="Y" />

The output can be i18n? I need the month in Spanish.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sure. Set your locale to Spanish and then use format="%m"

https://docs.grabaperch.com/docs/templates/attributes/type/date/