Forum

Thread tagged as: Question, Problem, Runway

Passing dynamic variable into category template

Hi,

I have a category set called Styles which needs to be mulitlingual, and is set up as follows, modifying the regular category.html template to include fields for language variations :


<perch:before><ul></perch:before> <li style="margin-left: <perch:category id="catDepth" type="hidden" />0px;"> <h4><perch:category id="catTitle" type="smarttext" label="Title" required="true" /></h4> <perch:category id="catSlug" type="slug" for="catTitle" suppress="true" /> <perch:category id="desc" type="textarea" label="Description" editor="markitup" markdown="true" size="s" /> <perch:category id="catTitleEn" type="smarttext" label="Title En" required="true" /> <perch:category id="catSlugEn" type="slug" for="catTitleEn" suppress="true" /> <perch:category id="catTitleEs" type="smarttext" label="Title Es" required="true" /> <perch:category id="catSlugEs" type="slug" for="catTitleEs" suppress="true" /> <perch:category id="catTitleFr" type="smarttext" label="Title Fr" required="true" /> <perch:category id="catSlugFr" type="slug" for="catTitleFr" suppress="true" /> </li> <perch:after></ul></perch:after>

Within a collection template, I need to output this category info. I can get the output if I specifically ask for ids used in the template. For example, the following will output the Spanish:

<perch:categories id="restaurant_styles" set="styles">
<perch:before><ul></perch:before>
<li><a href="/<perch:category id="lang" />/<perch:category id="catSlugEs" />"><perch:category id="catTitleEs" /></a></li>
<perch:after></ul></perch:after>
</perch:categories>

But I need the output to be dynamic so I can output the right language when I need it.

I have a variable to indicate the current language and I can create a variable to match the field name that I need. I've tried setting this as a system variable and then passing it into the template:

$currentCatTitle = 'CatTitle' . $lang;
PerchSystem::set_var('currentCatTitle',$currentCatTitle);

I tried using the following <perch:category id="currentCatTitle" /> but it outputs the value of the variable currentCatTitle, and I also tried <perch:category id="{currentCatTitle}" /> which doesn't output anything.

So my question is: is there are way to achieve this dynamically?

(I could resolve this by adding in enough <perch:if> conditionals for each language but I'd prefer a cleaner solution if possible.)

Many thanks in advance for help or suggestions.

Simon Kelly

Simon Kelly 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tried <perch:showall> ?

Hi Drew,

Yes, and it shows that currentCatTitle is available.

I can use it to output the value of currentCatTitle to the page with <perch:category id="currentCatTitle" /> . But I was hoping that <perch:category id="{currentCatTitle}" /> would evaluate the variable so that I can use it dynamically. Is that not a behaviour that I can expect?

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't understand what you're asking. Can you give an example of how you're trying to use it?

I have a category set called Styles that allows me to provide category names in multiple languages. In addition to the fields in the default category.html´ template, I have added fields calledcatTitleEn,catTitleEs,catTitleFr`, and here I can enter my multilingual options for example Rustic, Rústico, Rustique.

I have a collection called Facts where users can assign a category from this set to the collection item as follows:

<perch:categories id="restaurant_styles" label="Styles" set="styles" display-as="checkboxes">

I have another collection called Restaurants where I can assign a category from the Styles category set in the Facts collection:

<perch:related id="restaurants_facts" collection="Restaurants Facts" label="Facts" max="1" required="true">
<perch:categories id="restaurant_styles" label="Styles" set="styles" display-as="checkboxes">
</perch:related>

To output a list of all the Styles categories assigned to an item in the Restaurants collection, I use a template ´_restaurants-listing.html´ with ´perch_collection´. This template can show all the English titles with the following:

<perch:related id="restaurants_facts" collection="Restaurants Facts">
<perch:categories id="restaurant_styles" set="styles">
<perch:before><ul></perch:before>
<li><perch:category id="catTitleEn" />
<perch:after></ul></perch:after>
</perch:categories>
</perch:related>

Or I can show all the Spanish title by adjusting it to:

<perch:related id="restaurants_facts" collection="Restaurants Facts">
<perch:categories id="restaurant_styles" set="styles">
<perch:before><ul></perch:before>
<li><perch:category id="catTitleEs" />
<perch:after></ul></perch:after>
</perch:categories>
</perch:related>

But I don't want to hardcode the field name (catTitleES, catTitleEn etc). Instead I want this to be dynamic.

I can easily create a variable (for example currentCatTitle based on the language currently required that will match the name of the field I want to show (catTitleES, catTitleEn etc) and pass it into the template. But if I use this in the template as follows, the output to the page is the value of the variable:

<perch:related id="restaurants_facts" collection="Restaurants Facts">
<perch:categories id="restaurant_styles" set="styles">
<perch:before><ul></perch:before>
<li><perch:category id="currentCatTitle" />
<perch:after></ul></perch:after>
</perch:categories>
</perch:related>

So in English will see on screen a <ul> with each <li> as catTitleEn, whereas want I want on screen is <ul> with each <li> showing what is entered in the category template on the catTitleEn field.

I thought that I could achieve this by wrapping the variable name in brackets (see below) but I don't get any output at all there.

<perch:related id="restaurants_facts" collection="Restaurants Facts">
<perch:categories id="restaurant_styles" set="styles">
<perch:before><ul></perch:before>
<li><perch:category id="{currentCatTitle}" />
<perch:after></ul></perch:after>
</perch:categories>
</perch:related>

Is that any clearer? Are you able to suggest anything?

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, you can't do that. I'd suggest dynamically switching templates instead.

Do you mean calling _restaurants-listing-en.html or _restaurants-listing-es.html, for example? Yes, I could do that but if that's the case I can just use conditionals in a single template.

This would be incredibly useful for building out multilingual sites. I don't know if anyone else has come up against this, but if it were to be looked at in the future, I'd be very happy!

Thanks for your help.