klox7 VIP
Total posts: 914
01 Jul 2014 17:18

I hope I will ask this as celar as possible...

I have several content types with different fields and different number of fields. For all sections and content types I would like to use only one template. I know Cobalt already has core templates and you can use one for several types (using php foreach ($item->fields_by_id AS $field):) but I don't think that's what I want.

Examples:
* Type1 -> fields a, b, c, d, e
* Type2 -> fields 1, 2, 3, 4, 5
* Type3 -> fields a1, b2, c3, d4, e5

Let's say we have 3 divs - top, main, bottom.

I would like to display fields like this (fields have to corespond to content type):
* div.top -> fields d, e
* div.main -> fields a, b, 4, e5
* div.top -> fields 3, b2

So if I'm in record list of Content Type1 I would like to display in div.main only fields from this Type (a, b). Even though there are also other fields form other content types added template. If I'm in record list of Content Type2 then in div.main there is field 4.

Is there any good practice how to achieve this?

Last Modified: 20 Apr 2015


Sergey
Total posts: 13,748
13 Aug 2014 00:32

You can add key attribute

<field name="field_top" key="id" type="meresourcesfields" label="What do we show in TOP div?" />

Then ID will be saved and will not be changed on label update.

But you cannot to do it like this any more

$item->fields_by_key[$params->get('tmpl_params.field_top')]->result

You will have to do this

$key = $this->fields_keys_by_id[$params->get('tmpl_params.field_top')];
$item->field$item->fields_by_key[$key]->result

klox7 VIP
Total posts: 914
08 Sep 2014 10:05

I have this now:

<?php //image 
$image = $item->fields_by_key[$params->get('tmpl_params.field_image')];
if(!empty($image)):?>
    <div class="image <?php echo 'rslt-'.$image->id; ?> <?php echo $image->params->get('core.field_class');?>">
     <?php echo $image->result; ?>
     </div>
<?php endif; ?>

So with fields_keys_by_id it would be:

<?php //image 
$key = $this->fields_keys_by_id[$params->get('tmpl_params.field_image')];
if(!empty($key)):?>
    <div class="image <?php echo 'rslt-'.$key->id; ?> <?php echo $key->params->get('core.field_class');?>">
     <?php echo $item->field->$item->fields_by_key[$key]->result; ?>
     </div>
<?php endif; ?>

?


Sergey
Total posts: 13,748
09 Sep 2014 01:57

Note that $params->get('tmpl_params.field_image') have to contain ID.

<?php
$key = $this->fields_keys_by_id[$params->get('tmpl_params.field_image')];
if(!empty($item->fields_by_key[$key]->result)):?>
    <div class="image <?php echo 'rslt-'.$key->id; ?> <?php echo $key->params->get('core.field_class');?>">
     <?php echo $item->fields_by_key[$key]->result; ?>
     </div>
<?php endif; ?>

klox7 VIP
Total posts: 914
09 Sep 2014 10:06

Sergey Note that $params-&gt;get('tmpl_params.field_image') have to contain ID.

You mean key="id" in xml parameters?

The upper solution doesn't work for me. It breaks site. No errors in reporting.

key_id


Sergey
Total posts: 13,748
11 Sep 2014 02:06

In this line

<div class="image <?php echo 'rslt-'.$key->id; ?> <?php echo $key->params->get('core.field_class');?>">

should be like this

<div class="image <?php echo 'rslt-'.$key ?> <?php echo $item->fields_by_key[$key]->params->get('core.field_class');?>">

Because $key is not an object.

Powered by Cobalt