1

First of all, Please excuse my English

Q: I have a section called "Events" And and has some entries

On the front-end i have a list of card "Events" and each card has a "Follow" button to follow the event and when the "event" time is on the word "follow" will be replaced with "Attend" after clicking the btn, if it makes sense

How to do that in Craft with twig?

I found some references in PHP and someway understood how it works with Ajax, but never understood how it's done with Craft?

The path where i put js/php and where to send action request? How to save event following number...etc

Thank you all.

4

This functionality is comprised of multiple components – you need to build the interface, keep track of which visitor follows which events, potentially do something with that information and change up the display depending on the current time.

The first thing you should do is clear up the requirements – what happens when someone follows an event? What happens when someone clicks on "attend"? How are event follows attributed to people across sessions – with cookies, with a login system? Maybe all that stuff is in your head, but since you don't mention any of it it's hard to give a useful answer. And if you can't explain this feature concisely in a way that makes sense to someone unfamiliar with your project, odds are you don't fully understand the problem yourself.

That said, here are a couple of pointers to get you started:

On the front-end i have a list of card "Events" and each card has a "Follow" button to follow the event

Outputting a button in Twig is trivial, the question is what happens when someone clicks the button? If it's only stored locally in the browser, all you need is some JavaScript to handle clicks on that button and maybe store the followed events in local storage.

I assume you want to keep track of which people follow which events. First you need to determine how to identify visitors. If your visitors are logged-in Craft users, that's easy, but for anonymous sessions you need some other method (like setting a cookie).

Then you need to build a Controller with an endpoint to handle event follows. The controller can contain the logic to process and store the event followers. Once you have your controller, you can either make the button a form that posts to the controller's action URL or handle button clicks in JavaScript and call the action URL manually.

Check out the documentation on Controllers to get started.

when the "event" time is on the word "follow" will be replaced with "Attend" after clicking the btn, if it makes sense

That's easy, in your Twig template for the event page, simply check if the event is currently active (i.e. the current time is between the start and end time of the event) and display the follow or attend buttons accordingly. Of course, the real question is what happens when either button is clicked – again, this is something you have to figure out as part of gathering requirements.

2
  • 1
    Thank you for your time and explanation it was a great starting point for me to read and learn more about how things are working in the background to achieve such things. i'm starting reading about Controllers, Creating a Module ...etc so when i have a good background about it i will return to this question and post an edit. Thank you again.
    – Raad Kasem
    Nov 25 '21 at 6:35
  • @RaadKasem Glad I could help! If you do run into problems, its probably better to create a new question, it's easier to answer specific smaller questions and it helps other people find answers quicker.
    – MoritzLost
    Nov 25 '21 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.