Skip to content

View Helpers

previousPageLink()

previousPageLink() — returns string

Available in: controller Category: Pagination Functions

Creates a link to the previous page, or a disabled span when on the first page.

NameTypeRequiredDefaultDescription
textstringnoPreviousThe text for the link.
handlestringnoqueryThe handle given to the query that the pagination should be displayed for.
namestringnopageThe name of the param that holds the current page number.
classstringno—CSS class for the link element.
disabledClassstringnodisabledCSS class for the disabled span element.
showDisabledbooleannotrueWhether to render a disabled span when on the first page.
pageNumberAsParambooleannotrueDecides whether to link the page number as a param or as part of a route.
encodeanynotrueUse this argument to decide whether the output of the function should be encoded in order to prevent Cross Site Scripting (XSS) attacks. Set it to true to encode all relevant output for the specific HTML element in question (e.g. tag content, attribute values, and URLs). For HTML elements that have both tag content and attribute values you can set this argument to attributes to only encode attribute values and not tag content.
//--------------------------------------------------------------------
// Example 1: Basic usage — show a "Previous" link above a paginated list;
// renders a disabled span when already on the first page

// Controller code
param name="params.page" type="integer" default="1";
posts = model("Post").findAll(page=params.page, perPage=10, order="createdAt DESC");

// View code
<cfoutput>
    #firstPageLink()#
    #previousPageLink()#
    #pageNumberLinks()#
    #nextPageLink()#
    #lastPageLink()#
</cfoutput>


//--------------------------------------------------------------------
// Example 2: Custom link text and CSS classes

// View code
<cfoutput>
    #previousPageLink(
        text="&laquo; Previous",
        class="page-link",
        disabledClass="page-link disabled"
    )#
</cfoutput>


//--------------------------------------------------------------------
// Example 3: Hide the disabled element entirely when on the first page

// View code
<cfoutput>
    #previousPageLink(showDisabled=false)#
</cfoutput>


//--------------------------------------------------------------------
// Example 4: Use a named route so page numbers appear in the URL path
// instead of as a query-string param (e.g. /articles/page/2)

// Route setup in app/config/routes.cfm
mapper()
    .get(name="paginatedArticles", pattern="articles/page/[page]", to="articles##index")
    .get(name="articles", pattern="articles", to="articles##index")
.end();

// Controller code
param name="params.page" type="integer" default="1";
articles = model("Article").findAll(page=params.page, perPage=20, order="title");

// View code
<cfoutput>
    #previousPageLink(route="paginatedArticles", pageNumberAsParam=false)#
</cfoutput>