JReviews logo Docs
Menu
Version

render

Generate a different output for controller actions using the `render` method.

Filter
System
Since 3.8.0

You need to have a working knowledge of Hooks before you get started.

The filter has two additional variations to allow targeting a specific controller and controller action without the need for additional checks.

  • render_{name}
  • render_{name}_{action}

Where {name} is the controller name and {action} is the controller method.

Parameters

Name Type Description
$output parameter (string) return non-empty string to use as the output for the request
$params parameter (array) associative array with contextual data
$instance parameter (MyController) instance of current controller class

Boilerplate Code

Use the boilerplate code to start using the filter, and add your own logic to modify the first argument and return it.

Clickfwd\Hook\Filter::add('render', function($output, $params, $instance)
{
    // Uncomment line below to view available data passed into hook
    // fwd_dd($output, $params, $instance);

    return $output;
});
Development & Support
Customizations are not included with support. We provide this information to make it easier for developers to extend the functionality. From time to time we may have some availability for custom work. Get in touch to see if there's an opportunity to work together.

Examples

Build a read-only REST API

<div class="markdown"> By skipping the template processing altogether, you can build a REST API using this filter. Read the [Building and Consuming a Read-Only REST API](https://www.jreviews.com/blog/building-and-consuming-a-read-only-rest-api) blog post. </div>

Restrict listing detail visibility to owner and certain groups

Clickfwd\Hook\Filter::add('render_com_content_com_content_view', function($output, $params) 
{
	// Change IDs to restricted category IDs
	$restrictedCatIds = [1,2,3];

	// Change IDs to authorized group IDs
	$authorizedUserGroups = [1,2,3];

	$user = S2App::make('auth');

	$listing = $params['viewVars']['listing'];

	$catId = $listing['Category']['cat_id'] ?? null;

	if (!in_array($catId, $restrictedCatIds)) {
		return $output;
	}

	// Checks current user against listing owner or user group to grant read-access
	if ($user->matchesUserId($listing['User']['user_id']) || $user->belongsToGroups($authorizedUserGroups)) {
		return $output;
	}

	// What you do here is up to you. Show a message, redirect, etc.
	// And it will affect how search engines view or don't view the page

	// Joomla - uncomment line below for redirect, and change URL and status as needed
	// Joomla\CMS\Factory::getApplication()->redirect('/', 403);
  
  	// WordPress - uncomment line below for redirect, and change URL and status as needed
  	// wp_redirect('/', 403);
  	
	// die;

	return 'No access';
});

Restrict listing detail visibility to logged in users

<div class="markdown"> Similar to the above example, but we render the signup template for guests instead of the listing detail page. </div>

Clickfwd\Hook\Filter::add('render_com_content_com_content_view', function($output, $params) 
{
	// Change IDs to restricted category IDs
	$restrictedCatIds = [1,2,3];

	$user = S2App::make('auth');

	$listing = $params['viewVars']['listing'];

	$catId = $listing['Category']['cat_id'] ?? null;

	if (!in_array($catId, $restrictedCatIds)) {
		return $output;
	}

	// Logged in users can see the listing detail page
	if ($user->connected) {
		return $output;
	}

  	// To pass additional variables to the view
  	$instance->set(['foo'=>'bar']);
  
  	// To read the current view suffix
  	$suffix = $instance->viewSuffix();
  	
  	// To change the view suffix
  	// $instance->viewSuffix('_products');

  	// Render view from elements/_sign up template
  	// To change the layout, you can pass a 3rd parameter with the layout name
	return $instance->view('elements', '_signup');
});

Source Files

  • /controllers/my_controller.php