1

Ok I have a file that I am loading that is being used for generating an email.

Is there a way to pass it variables, and to populate the file with the values of the variables, and THEN plug it into the message body to send?

Here is simple controller code:

$var = 'test';    
$ci->load->file('Test/view/dialog.php',true);

HTML file:

<strong><?php echo $var; ?></strong>

Note that dialog.php file is in root directory in "Test" dir, so I can't use $this->load->view();

I looked in Loader.php and It seems that file() method can't do that. Maybe there is any other solutions to do this?

Sorry for bad English language

1

3 Answers 3

3

I suggest making a template view file, and parsing it using the parser library, injecting the result of that into your message body. This way, you're not mixing PHP with your html. http://codeigniter.com/user_guide/libraries/parser.html

Now, the example you gave becomes:

<?php
$this->load->library('parser');
$message_body = $this->parser->parse('myfile.php',array('var' => 'test'),TRUE); //passing True as the last parameter makes the parser return the string instead of passing it to the output class.
?>

The HTML file:

<strong>{var}</strong>
Sign up to request clarification or add additional context in comments.

2 Comments

Great suggestion, but does this answer his actual question?
As far as I can tell, it does. If I understood the question correctly, he wants to load a file, passing variables to it, that will be put into the HTML, which then will be put into the body of an email. What I see in Min2liz's sample code, is that he is direcyle inserting PHP variables into the HTML: <strong><?php echo $var ?></strong. This gets hard to maintain, and is more error prone than using templates. I didn't have time to elaborate on my answer, but I'll do that now.
1

Best approach to this is use

$emailBody = $this->load->view('file_name', $data, true);
//file_name of the view to load
//$data you want to pass
// true so it will return as string and not load to browser.

Comments

0

If you can't use a view. You could do it with output buffering in your controller:

$var = 'Test';

ob_start();

include('./Test/view/dialog.php');
$msg = ob_get_contents();

ob_end_clean();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.