shopify liquid: How does "form.posted_successfully?" work? - forms

I was reading this page and came across this code:
{% if form.posted_successfully? %}
<p class="quote">{{ 'contact.form.post_success' | t }}</p>
{% endif %}
What I don't understand is, as a server-side language, how does liquid know whether the form is submitted successfully or not?

There is a GET attribute that is applied to the page URL that is set to true if the form is successfully submitted -> ?contact_posted=true.
If that GET attribute is present in the URL address the if statement becomes true and it will show the success message.

Related

Filtering by tag shows all pages instead of tagged pages

With reference to the Wagtial recipe on tags, I have set up tags in my courses app. However, tag filtering does not seem to be working. The tags themselves seem to be working: I can select tags in the admin and show them on the page. I have a course page tag model that seems to be working properly:
class CoursePageTag(TaggedItemBase):
content_object = ParentalKey('CoursePage',
on_delete=models.CASCADE,
related_name='course_tagged_items')
I have filtering setup in the CourseListingPage model (with template = "courses/course_listing_page.html") like this:
# Get all courses
course_pages = (
CoursePage.objects.live().public().order_by("-first_published_at")
)
# Filter by tag
tag = request.GET.get('tag')
if tag:
course_pages = course_pages.filter(tags__name=tag)
context['course_pages'] = course_pages
return context
And in the template I have this:
{% for tag in page.tags.all %}
{{ tag }}
{% endfor %}
When I click a tag on a course page with the above template code, I do arrive at the parent course listing page, and the URL shows what seems to be the correct filtering format:
courses/course-listing/?tag=test
But I do not see a filtered list of pages. Instead I see all course pages. I suspect there is something very simple that I am doing wrong here.
Answering my own question: {% for course in course_pages %} is required in the template at the location of the listing of the filtered pages -- in my case, below the filtering code. So, I now have the filtering code, then the actual listing of the pages, which begins with:
{% for course in course_pages %}
{% with course=course.specific %}
... HTML/template code for page details (e.g. course.title, course.sub_title) ...
{% endfor %}
{% endwith %}
I didn't initially that realize that the filtering code only does the filtering; it doesn't actually show the results of the filtering. For a beginner like me, when the docs say that the code will filter the listing to only those pages with a matching tag name before passing this to the template for rendering, it's possible to misread this to mean that the template will show the filtered results without further steps. But that's not the case. The extra contextual code is required -- at least it was for me.

Use of liquid tags across different pages

Is there a way to summon the description of post B (which is stated in its front matter) in post A?
The liquid tag {{page.description}} summons only the description of post A. Is there a way to include an url or something, thus using liquid tags across different files?
Or is there a better way to do this?
Unfortunately, front-matter variables are only accessible from within that file.
Between these triple-dashed lines, you can set predefined variables (see below for a reference) or even create custom ones of your own. These variables will then be available to you to access using Liquid tags both further down in the file and also in any layouts or includes that the page or post in question relies on.
Jekyll documentation
However, you can work around this for posts by looping through every post and including only what you want using an if statement.
{% for post in site.posts %}
{% if post.title == "Desired Post Title" %}
{{ post.description }}
{% endif %}
{% endfor %}

How to order Wagtail tags

I tried to create tags for my Posts as described in doc (tutorial My first website). But i need to show these tags in specific order. Is there some simple way or i just need to create own class with Orderable?
I have assumed you have gotten to this point in the docs - Tagging Posts and want to present the view of your BlogPage with the tags in a special order (eg. alphabetical). Orderable is if you want to do more complex admin interaction with InlinePanels and ordering of related models, where you are asking the page editor to order related items themselves.
The tutorial has this code for your template blog_page.html:
{% if page.tags.all.count %}
<div class="tags">
<h3>Tags</h3>
{% for tag in page.tags.all %}
<button type="button">{{ tag }}</button>
{% endfor %}
</div>
{% endif %}
To work with a custom ordering of your tags, you will need to set up a way to send your ordered tags to the template context. The easiest way to do this is to have a method added to your BlogPage model, we will call this get_ordered_tags.
class BlogPage(Page):
date = models.DateField("Post date")
# other fields...
def get_ordered_tags(self):
"""Specific ordered list of tags."""
# ordered_tags = self.tags.order_by('?') # order randomly
ordered_tags = self.tags.order_by('name') # order by tag name
return ordered_tags
Further up the page you would have seen the docs on Overriding Context, we will do something similar to add ordered_tags to our Page's context.
We can then easily make one minor change to our template, just replace:
{% for tag in page.tags.all %}
With:
{% for tag in page.get_ordered_tags %}
So instead of getting the tags in their default order, we are getting them in our specific order.

symfony2 twig form registration included from external template

Be patient is my first question and my english is also poor ;P
Btw... im using fos for my website and all work fine, actually my problem is that i have the "pages" template made with twig and it have, at the bottom, a call to action button that slideDown an hidden div where i want to put my registration form.
I setup the hidden div and try to put inside my include:
{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}
obviously it dosn't work:
Variable "form" does not exist in kernel.root_dir/Resources/JuliusUserBundle/views/Registration/register_content.html.twig at line 2
probably for some reasons related to routing or firewall or security?
anyone have a solutions, suggestions or ideas for that?
thanks and cheers!
As error said, you need to define 'form' variable in your action, or you could try to render FOSUser registration action instead of this.
For example:
{% render(controller(FOSUserBundle:Registration:register")) %}
If you need to use include then you have to pass the form variable to your included template. Optionally you can render that template also from its corresponding controller.
So for the first case you have:
{% include("FOSUserBundle:Registration:register_content.html.twig") with {'form':form} %}
Where form here is the variable you pass from your own controller. Your second choice would be to render the FOSUB template like so:
{% render(controller("FOSUserBundle:Registration:register")) %}
Apparently i found a good solution using Edge Side Includes (ESI) includes that give me also additional benefits in cache control:
<esi:include src="http://localhost:8004/login" />

Keep form data after errors on basic Shopify contact form when form fields are in loop

I got the solution for keeping the field data when having error here.
But my form fields are in loop so when I use
{{form.varName_ofField}} it does not work.
Please help...
You can only access the form variable within a form object. For example:
{% form 'contact' %}
available here: {{form.email}}
{% endform %}
but not here: {{form.email}}