How to manually render/display fields inside templates ... and Upgrading custom Templates to work in v1.5.6+

  • Published by
    George Papadakis
  • Last modified
    18 June 2015
  • Up to date
    Yes
  • Profile concerned
    End user, Webdesigner, Developer
  • Concerns
    Template
  • Since Version
    1.5.6
  • Voting
    Average rating
    8 votes
    • 1
    • 2
    • 3
    • 4
    • 5
  • Favourites
    248 How to manually render/display fields inside templates ... and Upgrading custom Templates to work in v1.5.6+ /documentation/tutorials-english/69-templating-module-plugin-content/248-upgrading-custom-templates-to-work-in-v156.html
NON-manually display of a field

enter it in a template position

  • go to backend FLEXIcontent templates manager
  • click to edit the ITEM layout of you content type or the category layout of your category
  • then user drag and drop to place the field in a layout position (of the template), save









MANUALLY display of a field inside your template file (2 ways)


METHOD 1: Use render only position (Prefered for category view performance)
  • ADD fields to the 'renderonly' position (1) (creates field but does not show it)
  • Use inside your item(_html5).php or category_items(_html5).php
echo $item->fields['fieldname']->display;



METHOD 2: Do a manual creation of the field's HTML and then display it,
(2) by using getFieldDisplay()


Use inside your item(_html5).php or category_items(_html5).php

// item view or category view
$myfield_html = FlexicontentFields::getFieldDisplay($item, 'fieldname');

// use either:
echo $myfield_html;
echo $item->fields['fieldname']->display;



OR for category view
(outside the FOR loop for better performance (now and in future versions))

// Please notice the 'items', instead of 'item'
FlexicontentFields::getFieldDisplay($items, 'fieldname');

foreach($items ...) {
  ...
  // use inside ITEMS loop
  echo $item->fields['fieldname']->display;
  ...
}









Advanced usage


The getFieldDisplay, function signature is:

FlexicontentFields::getFieldDisplay(
  &$item_arr,
  $fieldname,
  $single_item_vals=null,
  $method='display',
  $view = 'item'
);

Parameters
---------------
$item_arr -- you can render the field for an array of items, but you can also pass 1 item
$fieldname -- name of field to render, e.g. 'tags', 'categories', 'created', 'field37'
$single_item_vals -- custom value when rendering field for 1 item
$method -- all FLEXIcontent fields support method 'display' other fields support more e.g. 'image' field
$view -- fields display different depending on view, possible values are:  item, category, module

Return
-------
string:  with HTML when a single item was given
array of strings: when multiple items were passed, the array is indexed with item id  ($item->id)

Examples for image field

$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display', 'item');
$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display_small', 'item');
$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display_medium', 'item');
$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display_large', 'item');
$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display_original', 'item');

// AND USE:
echo $html;
// OR USE:
echo $item->fields['fieldname']->display;

// AFTER CALLING getFieldDisplay(), you can also use these arrays
print_r($item->fields['fieldname']->thumbs_src['small']);
print_r($item->fields['fieldname']->thumbs_src['medium']);
print_r($item->fields['fieldname']->thumbs_src['large']);
print_r($item->fields['fieldname']->thumbs_src['original']);


Examples for file field:

$html =FlexicontentFields::getFieldDisplay($item, 'fieldname', null, 'display', 'item');

// AND USE:
echo $html;
// OR USE:
echo $item->fields['fieldname']->display;

// AFTER CALLING getFieldDisplay(), you can also use these arrays
print_r($item->fields['fieldname']->filedata);  // e.g. size, absolute paths, etc
print_r($item->fields['fieldname']->url]);
print_r($item->fields['fieldname']->abspath]);









NOTES

(2) Calling getFieldDisplay is not needed if a component parameter
Create Fields HTML (under "Performance Options")
is set to create ALL fields for current view

But avoid using it, because
- it will add unneeded CSS/JS to your page
- it will have a performance impact in category view, which will become serious if you display more that 50 or 100 items per page


(1) IF your template does not have this already, then:

1. edit the your item.xml AND category.xml files of your template and add it like this

renderonlypos

2. Clear Joomla cache (needed in J2.5 and maybe needed in J3+)

otherwise the new position will not appear in the layout