3

For instance, let's say I have a snippet of code, which I'd like to keep separate. for now, we'll call it snippet.php.

snippet.php would be a simple block of reusable HTML which would have php variables in it. Something like this:

<article>
    <h1>{$headline}</h1>
    <p>${$body}</p>
</article>

I'd like to be able to return this code from a function, along the lines of this:

function writeArticle($headline, $body){
    return "contents of snippet.php using the variables passed in to the function"
}

I know I could just use html in a string to return, but the actual snippet would be fairly complex, and I want it to be modular.

3
  • What an incredibly unhelpful comment. Thank you. Commented Feb 16, 2012 at 2:30
  • its exactly what you are trying to do, it couldn't be more on point. Commented Feb 16, 2012 at 2:32
  • Even if it is on point, it's entirely unhelpful. You could at least name one of these "very many template engines" that are currently available. Commented Feb 16, 2012 at 2:34

4 Answers 4

1

One method is using file_get_contents and str_replace

HTML:

<article>
     <h1>[-HEADLINE-]</h1>
     <p>[-BODY-]</p>
</article>

PHP:

function writeArticle($headline,$body){
      $content = file_get_contents("[add your html directory here]/file.html",true);
      $content = str_replace("[-HEADLINE-]",$headline,$content);
      $content = str_replace("[-BODY-]",$body,$content);

      echo $content;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

This looks like exactly what I'd like to do. Thank you.
Welcome! Glad to help you with this problem
1

You can use output buffering and include the file so the PHP variables get evaluated. However, since you are not using <?php PHP tags ?> you will need to wrap it in HEREDOC format (http://php.net/manual/en/language.types.string.php). Scroll down to Heredoc on the page.

snippet.php

$output = <<<HEREDOC
<article>
  <h1>{$headline}</h1>
  <p>{$body}</p>
</article>
HEREDOC;

function writeArticle($headline, $body){
     ob_start();
     include('snippet.php');
     $snippet = ob_get_clean();
     return $snippet
 }

Comments

1

I had the same question and ended up solving it like this. I feel like this is the cleanest approach. Just turn php on and off within the function.

<?php
function writeArticle($headline, $body){
?>

<article>
<h1><?php echo $headline; ?></h1>
<p><?php echo $body; ?></p>
</article>

    <?php
}
?>

writeArticle('foo', 'bar');

Comments

0

You could do this:

HTML DOCUMENT
Include('blockClass.php');
$block = new blockClass();
echo $bl = $block->block($headline, $body);     

CLASS DOCUMENT
 class blockClass{

    function block($headline, $body){

      $var ='<article>
              <h1>' . $headline . '</h1>
              <p>' . $body . '</p>
             </article>';
      return $var;
    }
}

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.