Forum

Thread tagged as: Question, Runway

Categories & Collections: Displaying current category title?

In Runway I have a page that lists out a collection base on category (below). How do I display the Title of the category that is being viewed? <perch:category id="catTitle" /> obviously won't work but is there something similar based on the category slug that needs to be used?

<?php
  perch_collection('Resources', [
    'category' => 'departments/'.perch_get('cat'),
  ]); 
?>
Dan Lee

Dan Lee 1 points

  • 6 years ago

I think you can use this in your template to get the catTitle:

<perch:categories id="the-id-here-from-resources-template" set="departments" >
    <perch:category id="catTitle" />
</perch:categories>

Alex

Dan Lee

Dan Lee 1 points

Thanks for the reply Alex, I tried the code below following your advice - but it still doesn't display the catTitle.

<perch:categories id="categories" set="departments" >
    <perch:category id="catTitle" />
</perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does it display?

Dan Lee

Dan Lee 1 points

It displays nothing.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Which version are you running? 2.7.10?

Dan Lee

Dan Lee 1 points

Yes 2.7.10.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's very odd. I'll see if I can reproduce it.

Dan Lee

Dan Lee 1 points

Source for the page showing where the Category Title output should be and the list of relevant categories:

<div class="row">

                           <div class="columns large-6 small-7 feed__meta">
                        <span class="cat__icon"><i class="ss-icon">&#x1F4C1;</i></span>
                        <h4>Resource Bank</h4>
                        <p>Tag: 
                            <perch:categories id="categories" set="departments" >
                                <perch:category id="catTitle" />
                            </perch:categories>
                    </p>

                    </div>

                               <div class="large-11 columns entry">
                                      <div class="row">

                            <?php
                                perch_collection('Resources', [
                                    'category' => 'departments/'.perch_get('cat'),
                                ]); 
                            ?>

                        </div>
                    </div>

</div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, I'll see if I can reproduce it.

Hi Dan,

From your code above, it looks like the <perch:categories... code is in the wrong place. It should be inside a content template.

Alex

Dan Lee

Dan Lee 1 points

Alex,

The code above is for a page acting as an archive for a particular category.

The following code is used for the Content template "resource.html':

<perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
     <a href="/resource-bank/category/<perch:category id="catSlug" type="slug" />" class="p-category">
           <perch:category id="catTitle" type="text" />
     </a>
</perch:categories>
Drew McLellan

Drew McLellan 2638 points
Perch Support

But the code is in a template, not in the page, correct?

Dan Lee

Dan Lee 1 points

For clarity here is my process (In Runway)...

  1. I created a collection called resources with a template (content/cpi/resource.html).
  2. I created a category called departments with template (categories/departments.html).
  3. I linked the departments category to resources collection
  4. I created one master page for showing all resources (pages/resource_bank.php)
  5. I created one master page for showing resources base on category (pages/resource-bank/archive.php)
  6. I added a URL route pattern for archive.php of "resource-bank/category/[slug:cat]"

It is working all fine - all I need to figure out is how to display the category title in the archive.php master page. I have supplied the code for resource.html, departments.html and archive.php so that it is clear.

categories/departments.html looks like this:

<li>
        <perch:if id="catSlug" match="eq" value="{current_category}">
            <a class="filter-nav active" href="/resource-bank/category/<perch:category id="catSlug" />">
        <perch:else />
            <a class="filter-nav" href="/resource-bank/category/<perch:category id="catSlug" />">
        </perch:if>


            <span class="cat__icon"><i class="ss-icon"><perch:category id="catIcon" /></i></span>
            <perch:category id="catTitle" />
        </a>
</li>

content/cpi/resource.html looks like this:

<div class="card">
                                    <h3><perch:content type="text" id="desc" label="Title" order="1" required="true" title="true" /></h3>

                                    <p>
                                      <perch:content id="body" type="textarea" label="Description" markdown="true" editor="markitup" required="true"  />
                                    </p>

                                    <div class="card__menu">
                                        <ul class="side-nav">
                                            <li>
                                                <a href="<perch:content id="file" type="file" label="File" order="2" />">
                                                    <span class="cat__icon"><i class="ss-icon">&#xEB01;</i></span>
                                                    Download File
                                                </a>
                                            </li>

                                            <li>
                                                <span class="cat__icon"><i class="ss-icon">&#x1F465;</i></span>
                                                <a href="mailto:<perch:content id="owner_email" type="text" label="Owner email" />">
                                                Contact 
                                                <perch:content id="owner_first_name" type="text" label="Owner first name" />  
                                                <perch:content id="owner_last_name" type="text" label="Owner last name" /></a>
                                            </li>

                                            <li>
                                                <a href="<perch:content id="file" type="file" label="File" order="2" />">
                                                    <span class="cat__icon"><i class="ss-icon">&#xE100;</i></span>

                                                <perch:categories id="categories" set="departments" label="Categories" display-as="dataselect">
                                                  <a href="/resource-bank/category/<perch:category id="catSlug" type="slug" />" class="p-category">
                                                    <perch:category id="catTitle" type="text" />
                                                  </a>
                                                </perch:categories>
                                            </li>
                                        </ul>
                                    </div>
                                </div>

pages/resource-bank/archive.php looks like this:

<div class="row">
                    <div class="columns large-6 small-7 feed__meta">
                        <span class="cat__icon"><i class="ss-icon">&#x1F4C1;</i></span>
                        <h4>Resource Bank</h4>
                        <p>Tag: 
                            <perch:categories id="categories" set="departments" >
                                <perch:category id="catTitle" />
                            </perch:categories>
                        </p>

                    </div>

                    <div class="large-11 columns entry">
                        <div class="row">

                            <?php
                                perch_collection('Resources', [
                                    'category' => 'departments/'.perch_get('cat'),
                                ]); 
                            ?>

                </div>
                 </div>
</div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

archive.php is a page. You can't use template tags in a page. They have to go in a template.

Dan Lee

Dan Lee 1 points

Thanks gents!

Still getting my head round how Perch works.