Forum

Thread tagged as: Question

template variables not passing

I am trying to send a custom variable to template page, below is my code.

pages/detail.php

    PerchSystem::set_var('custom_cat', 'collection');

    perch_content_custom('Collection', array(
      'page' => '/collection',
      'template' => 'collection/collections_detail.html',
      'filter' => 'slug',
      'match' => 'eq',
      'value' => perch_get('s'),
      'count' => 1,
    ));
    print_r(PerchSystem::get_vars());

content/collection/collections_detail.html page content

<perch:content id="custom_cat" />

I don't get any value in template. I am however getting value in print_r

I made sure "custom_cat" id was unique in the template.

sawan ruparel

sawan ruparel 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does <perch:showall /> output in the template?

<perch:showall /> shows "custom_cat" and the value correctly.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. So how is the output wrong?

When i try to output "custom_cat" value on the page. I get empty. below is the code I am using to print

<perch:content id="custom_cat" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can we see your template? If it's there in showall then it should be available.

Are you using repeaters or blocks and not bringing the top level variable into scope?

Here is my template code

<div class="main" role="main">
<div id="content" class="content full">
<div class="container">
<div class="row">
<div class="col-md-12" id="content-col">
<article class="single-artwork-content format-standard">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="single-artwork-images">
<perch:content id="main_image" type="image" label="Main Image" width="600" height="400" crop="true" suppress="true" />
<div class="artwork-slider">
<div id="artwork-images" class="flexslider format-gallery">
    <ul class="slides">
    <perch:repeater id="images" label="Images" order="6" />
        <li><img src="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />" alt="" style="width:100%" />
        <perch:if exists="image_caption">
            <p class="flex-caption"><perch:content id="image_caption" type="text" label="Caption" size="l" /></p>
        </perch:if>
        </li>
    </perch:repeater>
    </ul>
</div>
</div>
<div class="additional-images">
<div id="artwork-thumbs" class="flexslider">
    <ul class="slides">



    <perch:repeater id="images" label="Images" order="6" />
        <li><a href="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />"><img src="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />" alt=""></a></li>
    </perch:repeater>

    </ul>
</div>
</div> 
</div>



<div class="clearfix"></div>


</div>
<div class="col-md-6 col-sm-6">
    <h1 class="short"><perch:content title="true" id="title" type="text" label="Name" order="1" />
    <perch:content id="slug" type="slug" for="title" suppress="true" />
    </h1>
    <perch:if exists="by">
    <div class="meta-data artists-list">By <a href="#" rel="tag"><perch:content id="by" type="text" label="by" order="2" /></a></div>
    </perch:if>
    <div class="spacer-10"></div>
<div class="spacer-20"></div>
    <div class="post-content">
        <perch:content id="body" type="textarea" label="Content" textile="true" editor="redactor" html="true" order="3" />
    </div>
    <div class="spacer-20"></div>
    <div class="meta-data"><i class="fa fa-tags"></i> 

        <perch:categories id="Category" label="Category" set="Category" required="true">
          <perch:before>
            <div class="meta-data">
          </perch:before>
              <li><a href="/<perch:content id="custom_cat" />/category/?cat=<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
          <perch:after>
            </div>
          </perch:after>
        </perch:categories>

        <perch:categories id="Type" label="Type" set="Type" required="true">
          <perch:before>
            <div class="meta-data">
          </perch:before>
              <li><a href="/category/?cat=<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
          <perch:after>
            </div>
          </perch:after>
        </perch:categories>

    </div>
    <div class="spacer-20"></div>

</div>
</div>
</article>
<div class="col-md-12 col-sm-12">
<perch:content id="body_more" type="textarea" label="Content more" textile="true" editor="redactor" html="true" order="3" />
</div>
</div>
</div>
</div>
</div>
</div>
<style>
    #artwork-images .slides li { position: relative; }
    #artwork-images .slides li .flex-caption { width: 100%; height: auto; min-height: 50px; margin: 0; padding: 10px; top: auto; left: 0; bottom: 0; border: none; }
  </style>

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's scoping.

Use scope-parent="true" on your perch:categories tag and then reference it with parent.custom_cat

You are probably not going to like me and the reply... that didn't work. here is the template code

<div class="main" role="main">
<div id="content" class="content full">
<div class="container">
<div class="row">
<div class="col-md-12" id="content-col">
<article class="single-artwork-content format-standard">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="single-artwork-images">
<perch:content id="main_image" type="image" label="Main Image" width="600" height="400" crop="true" suppress="true" />
<div class="artwork-slider">
<div id="artwork-images" class="flexslider format-gallery">
    <ul class="slides">
    <perch:repeater id="images" label="Images" order="6" />
        <li><img src="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />" alt="" style="width:100%" />
        <perch:if exists="image_caption">
            <p class="flex-caption"><perch:content id="image_caption" type="text" label="Caption" size="l" /></p>
        </perch:if>
        </li>
    </perch:repeater>
    </ul>
</div>
</div>
<div class="additional-images">
<div id="artwork-thumbs" class="flexslider">
    <ul class="slides">



    <perch:repeater id="images" label="Images" order="6" />
        <li><a href="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />"><img src="<perch:content id="image" type="image" label="Image" width="600" height="400" crop="true" />" alt=""></a></li>
    </perch:repeater>

    </ul>
</div>
</div> 
</div>



<div class="clearfix"></div>


</div>
<div class="col-md-6 col-sm-6">
    <h1 class="short"><perch:content title="true" id="title" type="text" label="Name" order="1" />
    <perch:content id="slug" type="slug" for="title" suppress="true" />
    </h1>
    <perch:if exists="by">
    <div class="meta-data artists-list">By <a href="#" rel="tag"><perch:content id="by" type="text" label="by" order="2" /></a></div>
    </perch:if>
    <div class="spacer-10"></div>
<div class="spacer-20"></div>
    <div class="post-content">
        <perch:content id="body" type="textarea" label="Content" textile="true" editor="redactor" html="true" order="3" />
    </div>
    <div class="spacer-20"></div>
    <div class="meta-data"><i class="fa fa-tags"></i> 

        <perch:categories id="Category" label="Category" set="Category" scope-parent="true" required="true">
          <perch:before>
            <div class="meta-data">
          </perch:before>
              <li><perch:content id="parent.custom_cat" /> - <a href="/<perch:content id="parent.custom_cat" />/category/?cat=<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
          <perch:after>
            </div>
          </perch:after>
        </perch:categories>

        <perch:categories id="Type" label="Type" set="Type" required="true">
          <perch:before>
            <div class="meta-data">
          </perch:before>
              <li><a href="/category/?cat=<perch:category id="catPath" />"><perch:category id="catTitle" /></a></li>
          <perch:after>
            </div>
          </perch:after>
        </perch:categories>

    </div>
    <div class="spacer-20"></div>

</div>
</div>
</article>
<div class="col-md-12 col-sm-12">
<perch:content id="body_more" type="textarea" label="Content more" textile="true" editor="redactor" html="true" order="3" />
</div>
</div>
</div>
</div>
</div>
</div>
<style>
    #artwork-images .slides li { position: relative; }
    #artwork-images .slides li .flex-caption { width: 100%; height: auto; min-height: 50px; margin: 0; padding: 10px; top: auto; left: 0; bottom: 0; border: none; }
  </style>

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll need to use a perch:category tag in that context. showall will help with this again.

I finally got it to work. Here is the solution

<perch:category id="custom_cat" />

<perch:content id="custom_cat" /> does not work if i am within category loop

Thanks Drew, perch:showall was very helpful

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's what I was saying! Glad you got it working.