Not sure if it's possible, but can you use if statements inside a template?
So if a phone number does not have a value I don't want to display that sentence at all...
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8"/>
<title>{form_title}</title>
</head>
<body>
<p>You received the following message from {name} through the Gossip Cakes' contact form.</p>
<p>Their email is {email}</p>
<p>Their phone number is {phone}</p>
<p>The message: {message}</p>
</body>
</html>
I guess I could use straight php, but is there a method of returning the html of a view?
class MY_Parser extends CI_Parser{
}
Enjoy :D
Assuming you're using CI's built in parser, you have to prep all the variables beforehand. There is no support for conditions, variable assignment, or anything beyond loops and basic token replacement at this time.
To do this in CI, you'd have to prep the whole message, something like this in your controller:
Not a good solution. Personally I'd recommend Twig if you're looking for a nice template parser. Your idea of "I guess I could use straight PHP" is also a very good one.
Use the third param of
view()
like so:CI templates do have support for IF statements if you are creative. I use them frequently. You can even use them to prevent the template elements from being parsed if there is no data.
Consider these three example arrays:
And consider this is the relevant section in your html:
Using the three arrays, the respective output arrays one and three are as follows. (Using array two would print nothing as the control array is empty.)