0

I'm using a simple PHP template script which has a function sets the values to the key and later replace the template tags with the key.

<?php
    class Template {
        protected $file;
        protected $values = array();
        public function __construct($file) {
            $this->file = $file;
        }
        public function set($key, $value) {
            $this->values[$key] = $value;
        }
        public function output() {
            if (!file_exists($this->file)) {
                return "Error loading template file ($this->file).<br />";
            }
            $output = file_get_contents($this->file);

            foreach ($this->values as $key => $value) {
                $tagToReplace = "[@$key]";
                $output = str_replace($tagToReplace, $value, $output);
            }

            return $output;
        }

?>

Then I have this foreach loop, I wanted to set all of the $post_title data to post_title, then output all the values, but I only got one element of data within the foreach loop.

<?php foreach ($post_titles as $post_title): ?>
<?php $data->set("post_title", $post_title['post_title']); ?>
<?php endforeach; ?>

<?php echo $data->output(); ?>

If I edit above code in the foreach loop to <?php echo $post_title['post_title']; ?> I will get all of the elements from the variable $post_titles which is an array, however, other ways like using return or set values to post_title will only get me one element from the array. How can I get all of the data elements within the foreach loop, not by immediately echoing out, but saved into a variable for later use outside of the foreach loop.

4
  • In your loop, you're currently setting the key to post_title on each iteration. $this->values[$key] = $value will there for be the same as $this->values['post_title'] = $value on each iteration. That overwrites the post_title every time it's called and you will only end up with the last post title. Commented Jan 25, 2018 at 9:07
  • Thanks, I kind of knew this problem but need a solution. Commented Jan 25, 2018 at 9:15
  • The logic of what you're trying to do doesn't really make sense. You want to replace one placeholder with multiple post titles? Shouldn't you choose which page title (only one) you want to replace it with? What is the expected output? Commented Jan 25, 2018 at 9:17
  • I just want all of the elements(post titles) to be output and replaced in the template page, for clarity reasons I omitted other stuffs like post contents. Commented Jan 25, 2018 at 12:03

1 Answer 1

0

Edited: You have to call output function in foreach

<?php foreach ($post_titles as $post_title): ?>
<?php $data->set("post_title", $post_title['post_title']); ?>
$allData[] = $data->output();
<?php endforeach; ?>
<?php print_r($allData); ?>

Call output function in foreach with set function.

Remove square brackets from $output variable.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm assuming that $output is a string (a template) and that the OP just wants to replace some keys in it to some other value and not to create an array. The OP is even running an echo on it later.
I modified the function output() variable $output to $output[] and used print_r($data->output()) , I still only get the first array element like this: Array ( [0] => [@post_title] [1] => Array ( [0] => some title ) )
You need to call it in loop where you setting up data
@Smartpal Thanks, you helped me to learn a lot!! While this did output a numeric array which includes all of the post titles, how could I just get the post titles? Better yet, how can I get them and replace my template tags?
Use implode("," ,$allData); to get all of elements in string with comma-separated

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.