Forum

Thread tagged as: Question

How do I achieve this?

I cant quite figure out how to achieve the following using conditionals:

The editor of a page is presented with a choice

a) display the default testimonial quote in the region (which will be hard coded in).
b) Choose to add a new testimonial for the region instead, to replace the default one.

Please can someone give me some pointers? Are conditionals the best way?

Chris James

Chris James 0 points

  • 4 years ago

Something like this?

<perch:if exists="testimonial">
    <p><perch:content id="testimonial" type="text" label="Testimonial" help="Leave blank to use default testimonial" /></p>
<perch:else />
    <p>This is the hard-coded default testimonial.</p>
</perch:if>

Thanks Shane.

Well, thats what I thought, but its not working for me. Any idea why? This is what I have...

<perch:if not-exists="custom-testimonial">
    <blockquote id="custom-testimonial" class="footer small-12 columns">
        <div class="small-12 medium-8 medium-offset-1 columns">
            <p><perch:content id="quote" type="textarea" label="quote" markdown="true" editor="markitup" imagewidth="320" imageheight="240" /><br>
            <span><perch:content id="author" type="text" label="author" required="true" title="true" /></span><p>
        </div>
    </blockquote>
<perch:else />
    <blockquote id="default-testimonial" class="footer small-12 columns">
        <div class="small-12 medium-8 medium-offset-1 columns">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tempor ipsum in ultricies scelerisque. Aliquam vitae lacinia nisl.<br>
            <span>Author name</span><p>
        </div>
    </blockquote>
</perch:if>

Seems to me the first line should be <perch:if exists="custom-testimonial">

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Your id attribute cannot contain an underscore: https://docs.grabaperch.com/templates/attributes/id/

I believe that Rachel meant that your id attribute cannot contain a hyphen.

Thanks for the input guys. All noted, but still not working. This is what I have right now:

<perch:if exists="CustomTestimonial">
    <blockquote id="CustomTestimonial" class="footer small-12 columns">
        <div class="small-12 medium-8 medium-offset-1 columns">
            <p><perch:content id="quote" type="textarea" label="quote" markdown="true" editor="markitup" imagewidth="320" imageheight="240" /><br>
            <span><perch:content id="author" type="text" label="author" required="true" title="true" /></span><p>
        </div>
    </blockquote>

<perch:else />

    <blockquote id="DefaultTestimonial" class="footer small-12 columns">
        <div class="small-12 medium-8 medium-offset-1 columns">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tempor ipsum in ultricies 
  scelerisque. Aliquam vitae lacinia nisl.<br>
<span>Author name</span><p>
        </div>
    </blockquote>
</perch:if>

However, the problem is that if I add a custom testimonial, the default one appears instead! And I cant leave the custom testimonial text box and author blank, because Perch wont let me save text areas with no content.

Is there something about the logic here that Im not getting?

It looks like you don't have a region ID that matches the <perch:if> ID "CustomTestimonial". Change the ID in <perch:if> to one of your region IDs ("quote" or "author"), or change a region ID to "CustomTestimonial".

Also, Perch won't let you save a blank region only if it contains required="true"

Ok but if I have 'quote' and 'author' as regions, how do I add a region id to the Blockquote that wraps them? Does a region id have to be preceeded by '<perch:content...>'? if so, how do I close it? Sorry if Im not making sense!

<perch:content...> IS a region. <perch:if> is only aware of Perch content regions. Perch isn't aware of the IDs of your HTML elements (like blockquote). So, you need to match the ID of <perch:if> to one of your content regions, i.e. 'quote' or 'author'. The blockquote ID isn't needed.

Thanks, I understand that Shane, where I'm struggling is that I have 2 regions - quote and author, but I need to wrap them in another region that I can then give an id to, so that the 'if' statement can target the wrapper. How do I create a region that I can use as a wrapper, since regions don't seem to have closing tags?

Chris, I'm not sure I'm following your logic or definition of "region". If you change your first line to <perch:if exists="quote">, I'm pretty sure this will work just the way you want.

Boom! And so it does work nicely.

Sorry - I was assuming that the if statement would'nt work because there were 2 regions in the wrapper that I want to be conditional together.

Thanks very much for your help Shane, and your patience :)

Happy to help!