Forum

Thread tagged as: Question

Reusing An Inputted Variable In The Same Region?

I have a product listing region that asks for image, product name and description. The product name is asked for as:

<perch:content id="text" type="text" label="Product Name" required="true" title="true" />

In the same region I would like to use the product name before the words 'Feeding Guide', e.g. <h2>Product Name Feeding Guide</h2>

What Perch code snippet should I use to output the Product Name? I've tried <h2><?php perch_content('Product Name'); ?> Feeding Guide</h2> which doesn't work. I'm guessing it should involve the ID but how do I reference this correctly?

Many thanks.

Chris Jones

Chris Jones 0 points

  • 4 years ago

Hi,

You are right you need to re-use the ID. So something like:

your-template.html

<perch:content id="text" type="text" label="Product Name" required="true" title="true" />
...
<h2><perch:content id="text" type="text"/></h2>

Many thanks Mike, my first time using Perch, not PHP savvy and back against the wall re deadline! I'll give that a try.

Thanks again Mike, works fine. If I can pick your brains further...

The Feeding Guide is supposed to use javascript to reveal, however for each product region added a unique ID will be required:

<div class="paneWrap">              
     <h2><a href="#" title="view the Feeding Guide" class="reveal" id="reveal1">Product Name Feeding Guide</a></h2>
     <div class="pane" id="feedingGuide1">
        <ol>
        <li>Feed by weight not by volume.</li>
        <li>Adjust quantities of feed, according to condition, exercise and temperament.</li>
        <li>Make changes to diet gradually.</li>
    </ol>
      </div>
</div>
<script>
    $("#reveal1").click(function(){
       $("#reveal1").toggleClass( "on" );
       $("#feedingGuide1").slideToggle(500);
       return false;
    });
</script>

I've added a field to my region asking the admin to add a unique reference which would replace the number 1 in the above ID's and javascript.

<perch:content type="text" id="uRef" label="Unique Reference" required="true" help="e.g. cc1" />
<style>
#feedingGuide<perch:content id="uRef" type="text"/> {display:none;}
</style>

<div class="paneWrap">              
    <h2><a href="#" class="reveal" id="reveal<perch:content id="uRef" type="text"/>"><perch:content id="prodName" type="text"/> Feeding Guide</a></h2>
    <div class="pane" id="feedingGuide<perch:content id="uRef" type="text"/>">
        <perch:content id="feeding_guide" type="textarea" label="Feeding Guide" required="true" markdown="true" editor="simplemde" />
        </div>
</div>

<script>
        $("#reveal<perch:content id="uRef" type="text"/>").click(function(){
            $("#reveal<perch:content id="uRef" type="text"/>").toggleClass( "on" );
            $("#feedingGuide<perch:content id="uRef" type="text"/>").slideToggle(500);
            return false;
        });
</script>

So, the feeding guide is hidden however the javascript reveal link doesn't work...

Any help greatly appreciated?

Alternatively, rather than asking the Admin to enter a unique reference that is used in the Javascript and HTML template does Perch count the number of multiple items added and if so how can I draw this number in to my Javascript and HTML template?

Duncan Revell

Duncan Revell 78 points
Registered Developer

If you add <perch:showall /> to your template and then view the page, you will see all the ids available to use in your template. There should be an item index count in there.

<perch:content id="perch_item_index" />

Hi Duncan, Robert,

Many thanks for that. Did a search of the forum for unique IDs and came across Robert's answer. Thanks again. First time using Perch with no PHP knowledge but must admit it is going well!