Jump to content

Database Template: Indicating a specific field


Meddysong

Recommended Posts

The customary database recordRow templates collect all fields from that record and display them with:

            {{if count( $row->customFieldsForDisplay('listing') )}}
				<div class='ipsDataItem_meta'>
				{{foreach $row->customFieldsForDisplay('listing') as $fieldId => $fieldValue}}
					{{if $fieldValue}}
						{$fieldValue|raw}
					{{endif}}
				{{endforeach}}
				</div>
			{{endif}}

I might, though, want to place different fields in different parts of the template, rather than have them all together. We see an example of this in the Release Notes, where the Current Release tag appears in one part of the template but the Security Release one somewhere else:

4wawS.thumb.jpg.aaa1bcfd3000e1418530abfb

So, let's say that I have three fields: field_1, field_2 and field_3. If I would like to isolate field_1 somewhere else in a template, what's the code I would need? And how would I change the excerpt above so that it no longer looks for all field values for the record, but only field_2 and field_3? Would I just do the same as field_1 but twice, and with _2 and _3 substituted for _1? Or is there a way of setting it up as some sort of array?

Thanks!

Link to comment
Share on other sites

There are a couple of ways to solve this. For example: 

  • Delete the foreach loop altogether and call each field separately. (Using the code visible in the field settings)
  • Deactivate a specific field for the listing or view template in the field settings and then call it directly through its field name (e.g. “field_58”). This way, the field is available in the template even though it was deactivated for the specific view and won’t show up in the foreach loop. 
  • Keep the foreach loop and add an IF check to suppress a certain field and then output it at another place in the template. 
Link to comment
Share on other sites

To display only 1 field on it's own you just use the following code in the category overview:

{$row->customFieldDisplayByKey('fieldkey', 'display')|raw}

This shows the value of the field.

And if use the "frontpage" of the database you have to write $record instead of $row

{$record->customFieldDisplayByKey('fieldkey', 'display')|raw}

You have to replace the fieldkey with whatever you set the fieldkey to. 

For the other 2 fields you mentioned i would use the second choice Ralf H. wrote. Deactive one typ of fieldtype (listing, disply) for the field you want to disply together with the others and make use of the foreach code you posted above. 

Link to comment
Share on other sites

Hello again, fellas. And thanks again for your help.

You've sort of solved my problem except for one main issue: Taking this approach produces the label and value, and doesn't capture my custom formatting. I know that I could put the CSS classes into the template for each field, but this would use the same formatting on them all, irrespective of the value. It also indicates No Value if an optional field happens to be blank.

It's probably better if I illustrate. The records in my databases show jobs that need doing. There are three fields indicating:

- Who the job is allocated to. (Can take multiple values. The formatting is customised depending on the value. I don't output the label.)

- Whether the tasks is open or closed. (If open, we get an icon of an unlocked padlock on a green background. If closed, it's a closed padlock and the background is red. A change made in the template means that the display of other things in the row changes too; the background turns grey, font colours are faded, etc.)

- A target date by which to finish. (This is an optional field. It outputs the label and a value but at smaller screen sizes drops the label.)

tasks-1.thumb.jpg.fe492361f3fad963bd643c

If I just call the particular values, I get the following:

tasks-2.thumb.jpg.2bb7178e462d6341bb4a55

I could add formatting, of course. It wouldn't be difficult to add <span class="ipsBadge ipsBadge_style11 ipsPos_right">, for example, into the template. But I would like to keep my custom formatting, choosing whether or not to use the label, whether to use an icon, letting the value determine the colour of the background, and so on. And I would also like not to show anything if there's no field value.

Do you think such a thing would be possible? With the foreach loop my custom formatting is maintained, so could we borrow some elements of its approach? (I'm thinking of {{foreach $row->customFieldsForDisplay('listing') as $fieldId => $fieldValue}}                    {{if $fieldValue}} ... in the current template, though I couldn't make it work when trying to target one field.)

Thanks in advance!

 

Link to comment
Share on other sites

  • 3 weeks later...

 

  • Keep the foreach loop and add an IF check to suppress a certain field and then output it at another place in the template. 

Hello again, Ralf. I think this might be the only course of action for me, if I'm to keep my custom formatting.

I think what I'd need to do is use this snippet of code:

{{if count( $row->customFieldsForDisplay('listing') )}}
				<div class='ipsDataItem_meta'>
				{{foreach $row->customFieldsForDisplay('listing') as $fieldId => $fieldValue}}
					{{if $fieldValue}}
						{$fieldValue|raw}
					{{endif}}
				{{endforeach}}
				</div>
			{{endif}}

in several places within the template, but with an IF check to suppress certain fields. Could you amend that snippet for me, please, to demonstrate how I'd tell it to ignore, say, field_1 and field_2? Thanks!

Link to comment
Share on other sites

Yes, I know, but as I mentioned in an earlier post, if I call a specific field (which has custom formatting), then I get:

- The labels and values, where I might only want the values

- The label plus the value null in those instances where my field is blank because it wasn't a mandatory one

- No chance of any formatting whatsoever in those cases where I have to have elaborate custom formatting (because I colour-code based on the value)

My issue is that something about that loop means that my custom formatting is obeyed and fields which have no value for a particular row are skipped rather than included as null. If I call the field directly, then that doesn't happen.

My question, therefore, is how can I call a field such that the custom formatting is obeyed and null values aren't included?

Link to comment
Share on other sites

My question, therefore, is how can I call a field such that the custom formatting is obeyed and null values aren't included?

The different output types are explained in the field settings:

The field key can be used in HTML templates to fetch a specific field, for instance: {$record->customFieldDisplayByKey('my_key', 'listing')|raw}

The key must be unique for this database. If omitted, a key will be automatically generated. Valid values for the second parameter include 'listing', 'display', 'raw', and 'processed'.

You can also bypass all the field settings by calling the field by its ID. That gives you the database field content directly:

{$record->field_XXX}

You can also check for an empty field this way:

{{ if $record->field_XXX }}
    <a href="{$record->field_XXX}">We got a Link!</a>
{{endif}}

 

Link to comment
Share on other sites

Problem solved. Thank you to @Ralf Herrmann and @LukasGr. for taking the time to help me.

The issue is a non-issue really. As Ralf set out above, using a line based on

{$record->customFieldDisplayByKey('my_key', 'listing')|raw}

is the way to go.

The reason that I had problems even though I'd already tried that approach is that I thought the word listing as the second parameter corresponded to the template name, since the default template is called listing, so I changed it to the name of the template I was using. In that situation it was pulling out the value and stripping the formatting, since I presume the parameter listing means "use the formatting associated with Listing View Format". Since mine had a nonsense parameter there rather than the one needed, I didn't get the result I wanted.

It's now doing what I want:

5.thumb.jpg.354c7ee50478e75f2192e210ce0a

Thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...