Advertisement:

Author Topic: Passing a message from Source to Template  (Read 1062 times)

Offline Alex4108

  • Jr. Member
  • **
  • Posts: 131
Passing a message from Source to Template
« on: March 10, 2012, 08:08:31 PM »
So I'm stuck... yet again coding with SMF.

I have an addition to the Forum Shop mod, that allows me to give credits to just one user.  The page is structured a bit like this:

<input fields>
<submit button to index.php?action=actions;step=2>

From what I understand, this is a bit how the SMF System will run when I click Submit

1. Go to Sources, and run the function for step=2
2. Load the Template that the Source calls in step=2 (In this case, being ShopAdmin2)

What I need to do, is pass a message from when the Source runs (if everything was okay, any errors, etc.) to the Template page. 

Originally, I thought maybe a $context['blah']['blahblah'] = "MESSAGE"; method would work, but the Template was not too thrilled with that.
I also can't really set $_GET or $_POST values, so those are out of the question.

So my question, is how should I pass a message from a Source to a Template?

Offline live627

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 5,117
  • Gender: Male
  • Cat: Destroy!
    • live627 on Facebook
    • live627 on GitHub
    • live627 on LinkedIn
    • @live627 on Twitter
    • livemods
Re: Passing a message from Source to Template
« Reply #1 on: March 10, 2012, 08:33:58 PM »
Did you bring $context into scope?
Try not to become a man of success, but rather try to become a man of value.
- Albert Einstein

Offline Alex4108

  • Jr. Member
  • **
  • Posts: 131
Re: Passing a message from Source to Template
« Reply #2 on: March 10, 2012, 09:30:18 PM »
Did you bring $context into scope?

You mean
Code: [Select]
global $context;?

Yes

Offline Suki

  • Project Manager
  • SMF Super Hero
  • *
  • Posts: 14,824
  • Kaizoku Jotei
    • SMF mods
Re: Passing a message from Source to Template
« Reply #3 on: March 11, 2012, 09:47:26 AM »
Why you can't set get or post vars?

step is a get var.

You also don't need a separate function, you can manipulate the form data in the same function by doing:

if (isset($_GET['step']) && $_GET['step'] == 2)
{
// code to validate/manipulate the form data, here you can set a custom message too

if (something_went_wrong)
$context['custom_error'] = $txt['my_custom_error'];

}

and on your template you simple call $context['custom_error']   wherever you want it to appear, you set $context['custom_error'] as null by default (inside the same function) so it only appears if an error occur when someone hit the save button


If you want to use  separate page, then you need to specify your own action int he form and create that action in de action array on index.php or create it via hooks:

<form action=', $scripturl ,'?action=myownaction
Me encanta que vengas a verme
Me encanta que aplaudas mis pocas virtudes
Hay algo que debes saber
Sólo ves en mi lo que tu quieres ver