Vheissu / Ci-Smarty

Smarty templating engine integration for Codeigniter 3.0+ with inbuilt theming and asset management.
http://ilikekillnerds.com
179 stars 43 forks source link

Code running past parse method #20

Closed diarmidsloan closed 12 years ago

diarmidsloan commented 12 years ago

I understood that when the $this->parser->parse() method was called this would output the display. I thought this was the case in all apps which I'd written to date. However, it turns out in a recent development project that I have clauses governing whether a template should be shown, or whether execution should proceed, and the line after the parse is being reached - see below.

$this->parser->parse('website/templates/test.tpl', $this->view_data); echo "continuing here...";

The only way I can find round this is to add a "return" method after the parse. This works ok, but I'm just checking if this is the right way, or if I'm missing something?

Vheissu commented 12 years ago

I'm slightly confused as to what you are trying to achieve here, but am guessing you have an if statement and based on whatever criteria you have in your if statement you'll display a template otherwise do something else. Like the below example?

if ($variable == 'yes') { $this->parser->parse('website/templates/test.tpl', $this->view_data); }

return FALSE;

I think what you are asking is if parse should break that if statement with a return to stop the return outside of the IF statement from executing. If you look at the default parse method in Codeigniter, the template contents are being returned in the same way Codeigniter's core library returns them.

I don't see the harm in using return to prevent code outside of an if statement from being run.

diarmidsloan commented 12 years ago

Yes, I suppose I had always assumed (incorrectly, it appears) that the $this->parser->parse line was the end of the road, and somewhere within that method the output was displayed and execution ended. I was surprised to see execution continue after the parse line, probably because in the vast majority of code I've written the parse is actually the last line of code in my method, so the question never arose.

Vheissu commented 12 years ago

Yeah, I've been in the same situation as you and have always had my parser line at the end of whatever code I was executing. Strange how the append_output method doesn't have a return somewhere preventing output from continuing, really weird as it is the only method in the output class that allows you to display output. Adding in a return statement to the MY_Parser parse method probably wouldn't hurt considering the method isn't used for much else.