Before - Liquid Tag Reference
On this page
The before block tag runs its content before the main page template is rendered. Use it in a Liquid controller to extract parameters, validate input, execute actions (like adding to cart), and prepare data for downstream rendering.
:::note
This tag works only inside a Liquid controller template (at controllers/<controller>/<action>.liquid). It is a silent no-op outside a controller.
:::
When before runs
In a Liquid controller, execution flows through three phases:
{% before %}block — runs first- Standard action — the main controller logic
- Page rendering — the template output
{% after %}block — runs after rendering{% final %}block — runs after response sent
Syntax
```liquid
{% before %} {% assign product_id = current_request.params.id %} {% action “cart.add”, product_identifier: product_id, quantity: 1 %} {% params product_id: product_id %} {% endbefore %} ```
| Property | Value |
|---|---|
| Tag Name | before |
| Type | Block tag |
| Source | StoreConnect |
What’s available in before
current_request.params— query string and form POST parameters- All globals —
current_store,current_customer,current_cart,current_product, etc. - All tags —
{% params %},{% action %},{% variables %},{% redirect %},{% respond %}
Common tasks in before
1. Extract and validate parameters
```liquid
{% before %} {% assign product_id = current_request.params.product_id %}
{% if product_id == blank %} {% respond body: ‘{“error”: “product_id required”}’, status: 400, layout: false %} {% endif %}
{% params product_id: product_id %} {% endbefore %} ```
2. Execute a built-in action
```liquid
{% before %} {% assign quantity = current_request.params.qty | default: 1 %} {% action “cart.add”, product_identifier: current_product.id, quantity: quantity %} {% params quantity: quantity %} {% endbefore %} ```
3. Redirect on a condition
```liquid
{% before %} {% if current_customer == blank %} {% redirect to: current_store.account_login_path %} {% endif %} {% endbefore %} ```
4. Set variables for rendering
```liquid
{% before %} {% variables page_title: “Checkout”, show_progress: true %} {% endbefore %} ```
Key behaviors
- Runs before rendering — anything set in
beforeis available in the page template - Can redirect — a redirect in
beforeskips the standard action and all subsequent rendering - Can respond — a response in
beforeskips rendering entirely - Validation first — use
beforeto validate all input before executing actions
Redirects from before
A redirect in before is the most common flow: validate input, then either proceed to the standard action or redirect to an error page.
```liquid
{% before %} {% assign order_id = current_request.params.id %}
{% if order_id == blank %} {% redirect to: current_store.orders_path, alert: “Order ID required” %} {% endif %}
{% params order_id: order_id %} {% endbefore %} ```
If validation passes and no redirect occurs, the standard controller action runs next.
Related phases
{% after %}— runs after the standard action and page template render; use for responses or post-render cleanup{% final %}— runs after the response is sent; use for fire-and-forget work like analytics- See Liquid controller lifecycle for the complete flow
Was this article helpful?
Thanks for your feedback! It helps us improve our docs.