Advertisement:

Author Topic: Passing a message from Source to Template  (Read 984 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

  • Developer
  • SMF Hero
  • *
  • Posts: 4,819
  • Gender: Male
  • Cat: Destroy!
    • live627 on Facebook
    • @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

  • Developer
  • SMF Super Hero
  • *
  • Posts: 13,960
  • Kaizoku Jotei
    • Free 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
No todos son tán malos, no todo está mal
No todos son villanos queriéndote matar
No todo está perdido ni se va a acabar
La vida es un picnic.