Templating

# Available Variables

Variable
craft.follow.followUrl()
craft.follow.unfollowUrl()
craft.follow.check()
craft.follow.following()
craft.follow.followingTotal()
craft.follow.followers()
craft.follow.followersTotal()

# followUrl

Value Description
Element ID (Required) The element you want the user to follow e.g. category.id

To allow the currentUser to follow an element you can use the followUrl method to output the correct URL. You simply pass an element ID to it, whether this is a user, category or tag ID:

<a href="{{ craft.follow.followUrl(user.id) }}">Follow User</a>

# unfollowUrl

Value Description
Element ID (Required) The element you want the user to unfollow e.g. category.id

To then let the currentUser unfollow an element you can use the unfollowUrl method to output the correct URL. You simply pass an element ID to it, user, category or tag ID:

<a href="{{ craft.follow.unfollowUrl(user.id) }}">Unfollow User</a>

# check

Property Value Description Default
elementId (Required) Element ID The element your checking the user is following -
userId User ID The user you want to check is following the element currentUser.id

If you want to check to see if the currentUser is following or not following a particular element (A Category, User etc) use the check method. You simply pass an element ID to it, e.g. a user, category or tag ID:

{% if craft.follow.check({ elementId: category.id }) %}
   <a href="{{ craft.follow.unfollowUrl(category.id) }}">Unfollow Category</a>
{% else %}
   <a href="{{ craft.follow.followUrl(category.id) }}">Follow Category</a>
{% endif %}

You can optionally pass a user ID to the check method to check if a different user is following a particular element:

{% if craft.follow.check({ elementId: category.id, userId: user.id }) %}
   {{ user.firstName }} is following {{ category.title }}
{% else %}
   {{ user.firstName }} is not following {{ category.title }}
{% endif %}

# following

Property Value Description Default
userId User ID The user you want to get a following list from currentUser.id
elementClass Element Class The element class you're checking the user is following craft\\elements\\User
output string or array The type of output string

WARNING

It's important to always use a double back slash when defining element classes e.g. craft\\elements\\User

TIP

You can see a list of available element types on 'Craft 3 Documentation - Element Types'

To get a list of users the current logged in user is following you use the following method. Then pass it's results in to the correct Element Query, in this case craft.users.

{% set users = craft.follow.following() %}

{% for user in craft.users.id(users) %}
   {{ user.fullName }}<br />
{% endfor %}

By default the following method will always get followed elements based on the currentUser and the craft\elements\User class and output as a string, e.g. 43,12,54,7.

You can optionally pass different properties. E.g. If you wanted to display a different users following list, or if you wanted to get a list of all the categories a particular user is following:

{% set params = {
   userId: user.id,
   elementClass: 'craft\\elements\\Category',
   output: 'string'
} %}

{% set categories = craft.follow.following(params) %}

{% for category in craft.categories.id(categories) %}
   {{ category.title }}<br />
{% endfor %}

# followingTotal

Property Value Description Default
userId User ID The user you want to get a following list from currentUser.id
elementClass Element Class The element class you're checking the user is following craft\\elements\\User

To get the total number of elements a particular user is following, you can used the followingTotal method. By default this will get the currentUser user following total:

{{ craft.follow.followingTotal({ userId: currentUser.id }) }}

You can optionally pass different properties. E.g. If you wanted to display how many categories a different user was following:

{{ craft.follow.followingTotal({ userId: user.id, elementClass: 'craft\\elements\\Category' }) }}

# followers

Value Description Default
Element ID The element your checking a followers list for currentUser.id

To see what users follower a particular element, you can use the followers method. By default this will get the currentUser followers list. You can optionally pass a different element ID to it. Whether this is a user.id or category.id etc.

{% set users = craft.follow.followers(category.id) %}

{% for user in craft.users.id(users) %}
   {{ user.fullName }}<br />
{% endfor %}

# followersTotal

Value Description Default
Element ID The element your checking a followers list for currentUser.id

To get the total number of followers (users) a particular element has, you can used the followersTotal method. By default this will get the currentUser user follower total. You can optionally pass a different element ID to it. Whether this is a user.id or category.id etc.

{{ craft.follow.followersTotal(user.id) }}