6

I have a function that is controlling the output of my page:

$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class='media-desc'>{$desc}</div>";

I would like to include a file "box.php" inside that html that is defined in the $page variable. I tried this:

$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . include("box.php"); . "</div><div class='media-desc'>{$desc}</div>";

... but it didn't work. How can I put a php include inside of a variable?

4 Answers 4

13

from php.net

// put this somewhere in your main file, outside the
// current function that contains $page
function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

// put this inside your current function
$string = get_include_contents('box.php');
$page  = '<div class="media-title"><h2>{$title}</h2></div>';
$page .= '<div class="media-image">{$image}</div>';
$page .= '<div class="inlinebox">' . $string . '</div>';
$page .= '<div class="media-desc">{$desc}</div>';
Sign up to request clarification or add additional context in comments.

4 Comments

This function is for real? D: +1
I'm not sure what to do here, that $page variable was already inside of a function.
Hopefully the comments now illuminate the issue for you.
worked Perfect :) thanks for your time and efforts
9

How can I put a php include inside of a variable?

# hello.php
<?php
  return "Hello, World!";
?>

# file.php
$var = include('hello.php');
echo $var;

I would generally avoid such a thing though.

4 Comments

2 questions: 1) How does this answer my question? It doesn't show me how to put that include statement inside of a variable. 2) Why would you avoid such a thing?
@mattz: Actually this does answer the question, just think about what you see for a sec... he's echo ing the include.
@Stephen, but can I do that inside a variable definition like in my question? When I do I get an error: Parse error: syntax error, unexpected T_ECHO in...
@mattz: I've updated the answer to make it more obvious that it works in variable assignment. There's nothing fundamentally wrong with doing something like this, but it then becomes tempting to make that included file dependent on global or local scope data. Generally you are better off with a slightly more polished system that can handle sending the templates parameters that replace placeholders, and so on.
3

First, don't use a semicolon from inside the statement.
Second, wrap the include statement in parentheses.

$page = "<div class='media-title'><h2>{$title}</h2></div>
<div class='media-image'>{$image}</div><div class="inlinebox">" . 
(include "box.php") . "</div><div class='media-desc'>{$desc}</div>";

Finally: In the "box.php" file, you will need to do the following:

<?php
ob_start();

// your code goes here

return ob_get_clean();

EDIT: Some info about calling return outside of the function contest: PHP Manual - Return.

1 Comment

I tried this but nothing returns. To test it I put <?php ob_start(); echo "Testing"; return ob_get_clean(); ?> as the contents of my box.php, and it didn't do anything. I removed the ob_start and ob_clean and it outputted the word "testing" 4 times. Twice in the header, once above where it should have and once below where it should have.
2

Edit:

Don't know if this is useful, but i think that including a file to get a piece of HTML, is not a good option. It's not scalable. You could try with something like MVC. You could ask your controller to renderize the content of what you want.

$view = $controler->getElement('box');

$page = "<div class='media-title'><h2>{$title}</h2></div><div class='media-image'>{$image}</div><div class="inlinebox">" . $view . "</div><div class='media-desc'>{$desc}</div>";

Try to decouple your code.

I recommend you to take a look to some MVC Framework, in my opinion, the best one is CakePHP.

7 Comments

I agree with you about MVC, and CakePHP in particular is awesome. But in smallish projects (where I wanna get things done fast and move on to cool projects), I would use includes for HTML... All that said, it looks like the OP is using a templating system, and thus this is not a smallish project, and thus MVC should definitely help, and thus you get a +1.
Thanks for the response but I think this is a little over my head. I'm sure this is a very good way but I don't know what half the things you are talking about are :)
haha, ok. That's a good conclusion. I agree with you that for small projects using a framework is not a good decision. But, is good to have things "organized". I've been looking for a small routing framework, but, didn't find one. Then, i coded my own one for this kind of situations. Is important to code "regular" things just once, and then reuse this things.
@mattz: Maybe not today, maybe not tomorrow, but you should read up on MVC. It'll change the way you code.
Acronym nitpick: If your controller is "renderizing" something, then it's not "MVC" (or actually PMVC). It's matches MVP then. (Model-View-Presenter)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.