Difference between revisions of "Variables you can use in theme files"

From JReviews Documentation
Jump to: navigation, search
(Discovery through the Dump and Die function)
 
(28 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Listing Data ==
+
__TOC__
 +
 
 +
== Discovery through the Dump and Die function ==
 +
 
 +
If are looking into customizing theme files, one important trick you should learn is the ability to look into a complex array to find out what its contents are so you can access those contents directly. Below we provide a list of common variables that can be used in theme files, but it's not meant to be exhaustive. Using this trick should get you further.
 +
 
 +
'''To view all variables sent to the theme file''''
 +
 
 +
<source lang="php">
 +
dd($this->viewVars);
 +
</source>
 +
 
 +
'''In the listing detail theme, to view the $listing array''''
 +
 
 +
<source lang="php">
 +
<?php dd($listing); ?>
 +
</source>
 +
 
 +
'''In the listing list page themes, to view the $listings array''''
 +
 
 +
<source lang="php">
 +
<?php dd($listings); ?>
 +
</source>
 +
 
 +
'''Quickly access data that's deeply nested inside an array'''
 +
 
 +
Use the S2Array helper class with dot notation as follows:
 +
 
 +
<source lang="php">
 +
<?php echo S2Array::get($listing, 'Listing.title'); ?>
 +
</source>
 +
 
 +
== Accessing data in the listing array ==
 +
 
 +
The <span style="color: blue">$listing array</span> contains a lot of useful information about a particular listing.
 +
 
 +
It is used in these files:
 +
* /listings/detail.thtml
 +
* /listings/detail_tabs.thtml
 +
* /listings/detail_compact.thtml
 +
* /listings/listings_blogview.thtml
 +
* /listings/listings_blogview_compact.thtml
 +
* /listings/listings_tableview.thtml
 +
* /listings/listings_thumbview.thtml
 +
* /listings/listings_masonry.thtml
 +
* /modules/listings.thtml
 +
 
 +
In these files you can add any data from the $listing array. Here is a list of the most commonly used variables.
 +
 
  
 
{| class="wikitable" cellpadding="0" cellspacing="0"
 
{| class="wikitable" cellpadding="0" cellspacing="0"
Line 6: Line 54:
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Listing']['title'];?></source> || Listing Title
 
| <source lang="php"><?php echo $listing['Listing']['title'];?></source> || Listing Title
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['slug'];?></source> || Listing Alias
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['url'];?></source> || Listing URL. In Joomla you need to pass it through JRoute::_() to convert it into a SEF URL.
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Listing']['summary'];?></source> || Listing summary text
 
| <source lang="php"><?php echo $listing['Listing']['summary'];?></source> || Listing summary text
Line 12: Line 64:
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Listing']['text'];?></source> || Summary + Description
 
| <source lang="php"><?php echo $listing['Listing']['text'];?></source> || Summary + Description
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['media_count'];?></source> || Media count
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['photo_count'];?></source> || Photo count
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['video_count'];?></source> || Video count
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['audio_count'];?></source> || Audio count
 +
|-
 +
| <source lang="php"><?php echo $listing['Listing']['attachment_count'];?></source> || Attachment count
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Listing']['hits'];?></source> || Number of visits
 
| <source lang="php"><?php echo $listing['Listing']['hits'];?></source> || Number of visits
Line 19: Line 81:
 
| <source lang="php"><?php echo $Time->nice($listing['Listing']['modified']);?></source> || Modified date
 
| <source lang="php"><?php echo $Time->nice($listing['Listing']['modified']);?></source> || Modified date
 
|-
 
|-
| <source lang="php"><?php echo $listing['Listing']['category_image'];?></source> || Category Image
+
| <source lang="php"><?php echo $listing['Category']['cat_id'];?></source> || Category ID
 
|-
 
|-
| <source lang="php"><?php echo $listing['Listing']['section_id'];?></source> || Section ID
+
| <source lang="php"><?php echo $listing['Category']['title'];?></source> || Category Title
 
|-
 
|-
| <source lang="php"><?php echo $listing['Section']['title'];?></source> || Section Title
+
| <source lang="php"><?php echo $listing['Category']['slug'];?></source> || Category Alias
 
|-
 
|-
| <source lang="php"><?php echo $listing['Listing']['cat_id'];?></source> || Category ID
+
| <source lang="php"><?php echo $listing['Directory']['dir_id'];?></source> || Directory ID
|-
+
 
| <source lang="php"><?php echo $listing['Category']['title'];?></source> || Category Title
+
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Directory']['title'];?></source> || Directory Title
 
| <source lang="php"><?php echo $listing['Directory']['title'];?></source> || Directory Title
 +
|-
 +
| <source lang="php"><?php echo $listing['ListingType']['listing_type_id'];?></source> || Listing Type ID
 +
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['User']['user_id'];?></source> || Listing author's ID
 
| <source lang="php"><?php echo $listing['User']['user_id'];?></source> || Listing author's ID
Line 36: Line 100:
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['User']['username'];?></source> || Listing author's username
 
| <source lang="php"><?php echo $listing['User']['username'];?></source> || Listing author's username
 +
 +
|-
 +
| <source lang="php"><?php echo $listing['User']['email'];?></source> || Listing author's email
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Review']['user_rating'];?></source> || User rating
 
| <source lang="php"><?php echo $listing['Review']['user_rating'];?></source> || User rating
Line 41: Line 108:
 
| <source lang="php"><?php echo $listing['Review']['user_rating_count'];?></source> || Number of user ratings
 
| <source lang="php"><?php echo $listing['Review']['user_rating_count'];?></source> || Number of user ratings
 
|-
 
|-
| <source lang="php"><?php echo $listing['Review']['user_rating_exists'];?></source> || Returns true if user rating exists
+
| <source lang="php"><?php echo $listing['Review']['user_review_count'];?></source> || Number of user reviews
 
|-
 
|-
| <source lang="php"><?php echo $listing['Review']['review_count'];?></source> || Number of user reviews
+
| <source lang="php"><?php echo $listing['Review']['editor_rating'];?></source> || Editor rating
 
|-
 
|-
| <source lang="php"><?php echo $listing['Review']['review_count_exists'];?></source> || Returns true if user review exists
+
| <source lang="php"><?php echo $listing['Review']['editor_rating_count'];?></source> || Number of editor ratings
 +
|-
 +
| <source lang="php"><?php echo $listing['Review']['editor_review_count'];?></source> || Number of editor reviews
 +
|-
 +
| <source lang="php"><?php echo $listing['MainMedia']['media_info']['image']['url'];?></source> || Main Media URL
 
|-
 
|-
 
| <source lang="php"><?php echo $listing['Favorite']['favored'];?></source> || Number of users who added listing to their favorite list
 
| <source lang="php"><?php echo $listing['Favorite']['favored'];?></source> || Number of users who added listing to their favorite list
 +
|-
 +
| <source lang="php"><?php echo $listing['Claim']['approved'];?></source> || The listing's current claim status
 
|}
 
|}
 
  
 
== Conditionals ==
 
== Conditionals ==
  
You can use any of the above data to create conditionals, for example:
+
You can use any of the above variables to create conditionals, for example:
 
<source lang="php">
 
<source lang="php">
<?php if($listing['Review']['review_count_exists']): // if user review exists ?>
+
<?php if($listing['Review']['user_review_count']): // if user review exists ?>
 
   // display something
 
   // display something
 
<?php else: ?>
 
<?php else: ?>
Line 62: Line 134:
 
</source>
 
</source>
  
 +
== Accessing the Listing Type title ==
  
== Custom Fields ==
+
If you need to get the listing type title for a particular listing use the code below in listing detail pages:
  
To output certain custom field in detail.thtml read [[How to customize the layout of custom fields in Theme files]]
+
<source lang="php">
 +
<?php echo $this->listing_type->title;?>
 +
</source>
 +
 
 +
In list pages or modules where there's more than one listing you need to access the title directly from the ListingTypes service container like this:
 +
 
 +
<source lang="php">
 +
<?php echo $this->listing_type->getById($listing['ListingType']['listing_type_id'])['ListingType']['title'];?>
 +
</source>
 +
 
 +
The code above can also be written in a more readable way shown below:
 +
 
 +
<source lang="php">
 +
<?php
 +
$listingTypeId = $listing['ListingType']['listing_type_id'];
 +
$listingType = $this->listing_type->getById($listingTypeId);
 +
echo $listingType['ListingType']['title'];
 +
?>
 +
</source>
  
  

Latest revision as of 14:09, 29 April 2020

Discovery through the Dump and Die function

If are looking into customizing theme files, one important trick you should learn is the ability to look into a complex array to find out what its contents are so you can access those contents directly. Below we provide a list of common variables that can be used in theme files, but it's not meant to be exhaustive. Using this trick should get you further.

To view all variables sent to the theme file'

dd($this->viewVars);

In the listing detail theme, to view the $listing array'

<?php dd($listing); ?>

In the listing list page themes, to view the $listings array'

<?php dd($listings); ?>

Quickly access data that's deeply nested inside an array

Use the S2Array helper class with dot notation as follows:

<?php echo S2Array::get($listing, 'Listing.title'); ?>

Accessing data in the listing array

The $listing array contains a lot of useful information about a particular listing.

It is used in these files:

  • /listings/detail.thtml
  • /listings/detail_tabs.thtml
  • /listings/detail_compact.thtml
  • /listings/listings_blogview.thtml
  • /listings/listings_blogview_compact.thtml
  • /listings/listings_tableview.thtml
  • /listings/listings_thumbview.thtml
  • /listings/listings_masonry.thtml
  • /modules/listings.thtml

In these files you can add any data from the $listing array. Here is a list of the most commonly used variables.


<?php echo $listing['Listing']['listing_id'];?>
Listing ID
<?php echo $listing['Listing']['title'];?>
Listing Title
<?php echo $listing['Listing']['slug'];?>
Listing Alias
<?php echo $listing['Listing']['url'];?>
Listing URL. In Joomla you need to pass it through JRoute::_() to convert it into a SEF URL.
<?php echo $listing['Listing']['summary'];?>
Listing summary text
<?php echo $listing['Listing']['description'];?>
Listing description text
<?php echo $listing['Listing']['text'];?>
Summary + Description
<?php echo $listing['Listing']['media_count'];?>
Media count
<?php echo $listing['Listing']['photo_count'];?>
Photo count
<?php echo $listing['Listing']['video_count'];?>
Video count
<?php echo $listing['Listing']['audio_count'];?>
Audio count
<?php echo $listing['Listing']['attachment_count'];?>
Attachment count
<?php echo $listing['Listing']['hits'];?>
Number of visits
<?php echo $Time->nice($listing['Listing']['created']);?>
Created date
<?php echo $Time->nice($listing['Listing']['modified']);?>
Modified date
<?php echo $listing['Category']['cat_id'];?>
Category ID
<?php echo $listing['Category']['title'];?>
Category Title
<?php echo $listing['Category']['slug'];?>
Category Alias
<?php echo $listing['Directory']['dir_id'];?>
Directory ID
<?php echo $listing['Directory']['title'];?>
Directory Title
<?php echo $listing['ListingType']['listing_type_id'];?>
Listing Type ID
<?php echo $listing['User']['user_id'];?>
Listing author's ID
<?php echo $listing['User']['name'];?>
Listing author's real name
<?php echo $listing['User']['username'];?>
Listing author's username
<?php echo $listing['User']['email'];?>
Listing author's email
<?php echo $listing['Review']['user_rating'];?>
User rating
<?php echo $listing['Review']['user_rating_count'];?>
Number of user ratings
<?php echo $listing['Review']['user_review_count'];?>
Number of user reviews
<?php echo $listing['Review']['editor_rating'];?>
Editor rating
<?php echo $listing['Review']['editor_rating_count'];?>
Number of editor ratings
<?php echo $listing['Review']['editor_review_count'];?>
Number of editor reviews
<?php echo $listing['MainMedia']['media_info']['image']['url'];?>
Main Media URL
<?php echo $listing['Favorite']['favored'];?>
Number of users who added listing to their favorite list
<?php echo $listing['Claim']['approved'];?>
The listing's current claim status

Conditionals

You can use any of the above variables to create conditionals, for example:

<?php if($listing['Review']['user_review_count']): // if user review exists ?>
  // display something
<?php else: ?>
  // display something else
<?php endif;?>

Accessing the Listing Type title

If you need to get the listing type title for a particular listing use the code below in listing detail pages:

<?php echo $this->listing_type->title;?>

In list pages or modules where there's more than one listing you need to access the title directly from the ListingTypes service container like this:

<?php echo $this->listing_type->getById($listing['ListingType']['listing_type_id'])['ListingType']['title'];?>

The code above can also be written in a more readable way shown below:

<?php
$listingTypeId = $listing['ListingType']['listing_type_id'];
$listingType = $this->listing_type->getById($listingTypeId);
echo $listingType['ListingType']['title'];
?>