Forum

Thread tagged as: Question, Problem

Using Attributes in templates

Hi, Just want to check if this is possible.

What I would like to do is using one of my page attributes and pull it through into a template.

Basically I want to use this attribute

<perch:pages id="sub-cat-intro" label="Introduction" type="textarea" /> 

Here in the <before> section of my

<perch:before>
<!--- I WANT TO USE MY ATTRIBUTE HERE -->
<!-- FEATURED PRODUCTS -->
<section class="listing">
<ul>
    </perch:before>
    <li>
        <perch:if exists="product-image">
        <img src="<perch:content type="image" id="product-image" label="Image" width="258" height="167" crop="true" divider-before="Product Images"/>" class="product">
        <perch:else /><img src="/images/placeholder-2.png" alt="No Image" style="border: 1px solid #ccc;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box; width: 100%" class="product"/>
        </perch:if>
        <p><perch:content id="product-code" type="text"/></span><perch:if exists="product-code"> - </perch:if><perch:content id="product-name" type="text" title="true"/></p>
        <a href="<perch:content id="_page"/>?s=<perch:content id="slug" type="slug" class="entry-title" />">View Product</a>
    </li>
    <perch:after>
</ul>
</section>
<!-- FEATURED PRODUCTS ENDS -->
</perch:after>

Is this possible... I can only see documentation for using attributes in navigation templates, not content templates.

Nik Gill

Nik Gill 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to pass it in using PerchSystem::set_var()

https://docs.grabaperch.com/docs/templates/passing-variables-into-templates/

Thanks Drew... I've had a read and I'm a little confused to be honest.

So I need to put in a php call into my page

<?php 
    PerchSystem::set_var('introduction');
    perch_content_custom('Product', array(
        'template'=>'template.html'
    ));
?>

The pull that into my template with something like this?

<perch:content id="introduction" />

With the template, is this the attribute template i.e defaul.htmlt?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You're not giving a value:

 PerchSystem::set_var('introduction', 'with this value');

so you'd want something like

 PerchSystem::set_var('introduction', perch_page_attribute('introduction', [], true));

Thanks Drew... that looks to have nailed it. I had to delete the ,[] from your code to get it to work, but its pulling in the correct bit of text into the page now.

Hi Drew.

Right, that's working except... that it's also showing up where I don't want it.

This is part of list/detail page and I'm pulling this content into my listing.html template. I had assumed that when the listing was clicked and the template changes to product-detail.html then this wouldn't be shown (as I'm showing it in a different template)

Can you suggest a way I can commit this from my detail template or stop it appearing. I assume with the PerchSystem code I'm pulling this data into the region "product" rather than into a specific template and it's showing before the Region rather than as part of it?

Nik

Actually, forget that... I've just figured it out, by moving the PerchSystem Code into the list mode section of the list/detail code. Working perfectly now!

HI Drew

Sorry to open this one up again... I'm having a little trouble working with the content the PerchSystem has delivered into my template. What I want to do is style it up, but I doesn't go in where I want it to, so I can style it.

What I want to do is have it display inside a <section> tag, like so, this is how I have it in my template

<section><perch:content id="introduction"/></section>

but What I'm getting is the content appearing before the section tags I've wrapped it in within the template

So in effect, I get the result as if I'd done the following

<perch:content id="introduction"/>
<section></section>

Basically a line of unstyled text and then the empty tags I wanted it to be in between.

Any ideas how I can get it to show up where I want it?

Drew McLellan

Drew McLellan 2638 points
Perch Support

This bit concerns me:

I had to delete the ,[] from your code to get it to work

What did you do?

Well... this is the php, I'm using to pull that data in.

// List mode
          PerchSystem::set_var('introduction', perch_page_attribute('introduction', true));
          perch_content_custom('Product', array(
               'template' => 'listing.html',
          ));

Your suggestion contained those brackets in the perch_page_attribute section... when I used that as it was, I got a blank page. When I took them out, things worked ok.

This is the full list/detail code on that page.

<?php


     perch_content_create('Product', array(
          'template'  => 'product-detail.html',
          'multiple'  => true,
          'edit-mode' => 'listdetail',
     ));


     if (perch_get('s')) {

          // Detail mode
          perch_content_custom('Product', array(
               'template' => 'product-detail.html',
               'filter'   => 'slug',
               'match'    => 'eq',
               'value'    => perch_get('s'),
               'count'    => 1,
          )); 

     } else {

          // List mode
          PerchSystem::set_var('introduction', perch_page_attribute('introduction', true));
          perch_content_custom('Product', array(
               'template' => 'listing.html',
          )); 
     }

?>  
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you on PHP 5.3? If so:

PerchSystem::set_var('introduction', perch_page_attribute('introduction', array(), true));

You can't just miss arguments out - the order matters.

Spot on Drew, that's nailed it.

You are awesome... this is why I've been a perch customer for 5 years!