Skip to content

Controller

renderWith()

renderWith() — returns any

Available in: controller Category: Rendering Functions

Instructs the controller to render the given data in the format requested by the client. If the requested format is json or xml, Wheels automatically converts the data into the appropriate format. For other formats—or to override automatic formatting—you can create a view template matching the requested format, such as nameofaction.json.cfm, nameofaction.xml.cfm, or nameofaction.pdf.cfm. This function is especially useful in APIs, AJAX endpoints, or situations where you need to respond dynamically in multiple formats based on client preferences. You can also control caching, layout, HTTP status codes, and whether to return the result as a string for further processing.

NameTypeRequiredDefaultDescription
dataanyyesData to format and render.
controllerstringno[runtime expression]Controller to include the view page for.
actionstringno[runtime expression]Action to include the view page for.
templatestringnoA specific template to render. Prefix with a leading slash (/) if you need to build a path from the root views folder.
layoutanynoThe layout to wrap the content in. Prefix with a leading slash (/) if you need to build a path from the root views folder. Pass false to not load a layout at all.
cacheanynoNumber of minutes to cache the content for.
returnAsstringnoSet to string to return the result instead of automatically sending it to the client.
hideDebugInformationbooleannofalseSet to true to hide the debug information at the end of the output. This is useful, for example, when you’re testing XML output in an environment where the global setting for showDebugInformation is true.
statusstringno[runtime expression]Force request to return with specific HTTP status code.
1. Render all products in the requested format (json, xml, etc.)
products = model("product").findAll();
renderWith(products);

2. Render a JSON error message with a 403 status code
msg = {
    "status" : "Error",
    "message": "Not Authenticated"
};
renderWith(data=msg, status=403);

3. Render with a custom layout
products = model("product").findAll();
renderWith(data=products, layout="/layouts/api");

4. Render a view template from a different controller
data = model("order").findAll();
renderWith(data=data, controller="orders", action="list");

5. Capture the output as a string instead of sending it to the client
output = renderWith(data=products, returnAs="string");