Forum

Thread tagged as: Question

Pass a variable into a content template, to set item classes?

I'm wondering if there is an easy way to pass a variable into a custom content template? I've been able to do this with layouts and a layout variables but haven't been able to find a way to do it with a content template.

I have two call to action buttons that I use the same content template for. I want one to have the class left, and the other have the class right.

Right now the class is selected by the user "left or right" but I'd like to make this class hard coded into the content template to eliminate the possibility for user error. Again, I'm hoping to find a way to pass a variable into the "class" for the class of the link and eliminate the user selecting a class via radio button.

Here's my code below:

<a href="<perch:content id="home-cta-link" type="text" label="Link for Button" help="This is the address to where you want the button to to lead to when you click it. Make sure to include the “https:www.” so that the links work correctly."/>" 

class="<perch:content id="home-cta-class" type="radio" options="Left|left-cta, Right|right-cta" help="For the Left button make sure left is corrected.  For the right button make sure that right is selected."/>">

<span class="title-1"><perch:content id="home-cta-title-1" title="true" type="text" label="Line 1" help="Enter the text that you wish to appear in the first line of text on the call to action on the home page." /></span>

<span class="title-2"><perch:content id="home-cta-title-2" type="text" label="Line 2" help="Enter the text that you wish to appear in the second line of text on the call to action on the home page." /></span></a>
Justin Kaiser

Justin Kaiser 0 points

  • 7 years ago

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

You could do

<?php 
    PerchSystem::set_var('position', 'left-cta');
    perch_content_custom('Region Name', array(
        'template'=>'template.html'
    ));
?>

Then put the id position into your template

I did look that page over and haven't been able to have any luck with it.

I wonder if the issue is that I'm trying to have two different values for the same variable at the same time?

For the first call to action button the variable "btn-class" is being set to "left-cta". In the second instance, for the second call to action button the variable "btn-class" is being set to "right-cta". Is it possible to do this or can I only setup one value for the variable on the page?

The current behavior is that text being displayed is the variable label "btn-class". Neither of the values for the variable are showing up at all. I'm not sure why this is.

So I'm unsure of where to proceed from here. Thanks to anyone that can help me sort through this. :)

Code that's in my page:

 <div class="medium-6 columns">
           <?php 
           PerchSystem::set_var('btn-class', 'left-cta');
           perch_content_custom('Call to Action Button Left', array(
            'template'=>'home_call_to_action_button.html'
            ));
            ?>
          </div>
        </div>
        <div class="smallfullimg">
          <div class="medium-6 columns">
            <?php 
            PerchSystem::set_var('button-class', 'right-cta');
            perch_content_custom('Call to Action Button Right', array(
              'template'=>'home_call_to_action_button.html'
              ));
              ?>
            </div>

Code that's in my html content template:

<a href="<perch:content id="home-cta-link" type="text" label="Link for Button" help="This is the address to where you want the button to to lead to when you click it. Make sure to include the &ldquo;https:www.&rdquo; so that the links work correctly."/>" 

class="btn-class">

<span class="title-1"><perch:content id="home-cta-title-1" title="true" type="text" label="Line 1" help="Enter the text that you wish to appear in the first line of text on the call to action on the home page." /></span>

<span class="title-2"><perch:content id="home-cta-title-2" type="text" label="Line 2" help="Enter the text that you wish to appear in the second line of text on the call to action on the home page." /></span></a>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're setting btn-class in one place, button-class in another, and using neither of them in the template.

Drew thanks for the pointers. Obviously, I wasn't understanding how this works. :) After revisiting the documentation (again), I think I finally got it—so thank you for your patience.

I noticed that a field for the content tag containing the variable kept showing up in the edit area along with the fields for the other perch:content tags. I had to change the content tag containing the variable to type="hidden". Is that the correct way to do it if I simply wan to pass through the variable?

Rachel Andrew

Rachel Andrew 394 points
Perch Support