CodeIgniter Forums
Template parser - add some conditions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Template parser - add some conditions (/showthread.php?tid=23360)



Template parser - add some conditions - El Forum - 10-08-2009

[eluser]Unknown[/eluser]
Hello CI users!

I am pretty new to CI and I decided to develop some project using this great framework.

I have some troubles when parsing template. I have my controller file written and now I am trying to parse data from database to view file.

I was successfull but I have one problem.

I have some files uploaded and their name is stored in database. So for example somebody will upload file "picture.jpg", so it will be stored in MySQL. And the thing I need is to add condition to the template:

If the file has .jpg extension, output will be IMG HTML code. But if there will be .swf I can't keep the IMG HTML code because the .swf will not display. So if the uploaded file will be .swf, it should echo the OBJECT code etc.

All I need is somebody to tell me how to handle conditions (IF, else) while parsing template. Is that possible? I guess yes. But I don't know how for now Smile

Thank you
Tomas


Template parser - add some conditions - El Forum - 10-08-2009

[eluser]Michael Wales[/eluser]
Most people don't use the template parsing - we just use straight PHP. I'll post some sample code below, but I would like to mention if you don't know what type of file your user has uploaded until it's time to display that file - you may want to look into how you've developed your application a bit. Without knowing about your application it's hard to making recommendations.

Using the alternate syntax since I prefer it in view files:
Code:
<?php if (substr($filename, -3) === 'jpg'): ?>
  <img src="&lt;?php echo $filename; ?&gt;" />
&lt;?php elseif (substr($filename, -3) === 'swf'): ?&gt;
  &lt;object src="&lt;?php echo $filename; ?&gt;"&gt;&lt;/object>
&lt;?php endif; ?&gt;



Template parser - add some conditions - El Forum - 10-08-2009

[eluser]rogierb[/eluser]
Are you using the template parser class?

If so, normally no php can be put in the view file you parse. (as far as I know)
The workaround I use is to eval() the parsed view.


Template parser - add some conditions - El Forum - 10-08-2009

[eluser]Unknown[/eluser]
Well, thank you guys. I think I will use straight PHP.

I've just wanted to know if it is possible to include some conditions to view file when using template parser class.

Thank you.