Shopify Liquid:form.posted_successfully?工作原理及表单提交判断疑问
form.posted_successfully? Works in Shopify Liquid, and How Liquid Determines Form Success Hey there! Great question—let’s break this down step by step, since it’s a common point of confusion with Shopify’s Liquid templating.
1. What does form.posted_successfully? do?
form.posted_successfully? is a boolean property attached to Liquid’s form object. Its sole job is to tell you if the current page load is the result of a successful submission of the form you’re rendering.
Here’s the flow in action for a typical Shopify form (like a contact form or customer registration):
- A user fills out and submits the form.
- Shopify’s backend processes the submission: it validates the input (checks for required fields, valid email formats, etc.) and runs any necessary logic (sending a contact email, creating a customer account, etc.).
- If everything goes smoothly, Shopify redirects the user back to the original page (or a specified success page).
- When Liquid renders that page on the redirect,
form.posted_successfully?will returntrue—it’s pulling that status from the backend’s request context. If the page is loaded normally (not after a successful form submit), it returnsfalse.
You’ll usually see it used to show success messages, like this:
{% form 'contact' %} {% if form.posted_successfully? %} <div class="alert alert-success"> Thanks for reaching out! We'll respond to your message shortly. </div> {% else %} <!-- Show form errors if any --> {% if form.errors %} <div class="alert alert-error"> {% for error in form.errors %} <p>{{ error | escape }}</p> {% endfor %} </div> {% endif %} <!-- Form fields --> <input type="text" name="contact[name]" placeholder="Your name" required> <input type="email" name="contact[email]" placeholder="Your email" required> <textarea name="contact[body]" placeholder="Your message" required></textarea> <button type="submit">Send Message</button> {% endif %} {% endform %}
2. How does Liquid (a server-side template) determine form success?
First, a quick clarification: Liquid isn’t a full server-side language—it’s a templating engine that runs on top of Shopify’s Ruby on Rails backend. The heavy lifting of checking form success happens in Shopify’s backend, not in Liquid itself. Here’s the detailed breakdown:
- When you submit a form, the request goes to a Shopify backend controller (e.g.,
ContactControllerfor contact forms). - The controller validates the form data. If validation passes and the business logic runs without issues, it uses the Post/Redirect/Get (PRG) pattern: it redirects the user to the original page instead of rendering it directly. This prevents duplicate form submissions if the user refreshes the page.
- During that redirect, Shopify’s backend stores a "success" flag in the user’s session or flash storage (temporary request-specific storage).
- When the redirected request loads the page, the backend injects that success status into the Liquid
formobject. Liquid just exposes this status via theposted_successfully?property—you’re not writing logic in Liquid to check success, you’re reading a value the backend has already computed.
Once the page loads after the redirect, the success flag is cleared (or no longer tied to the request), so refreshing the page will set form.posted_successfully? back to false.
内容的提问来源于stack exchange,提问作者shenkwen

