Which could be the cause of a variable not being printing in an HTML template called from another PHP class? -


i'm developing base of mvc project in php. i'm advancing view part, working expected in rendering of html template.

here contents of files i'm coding make work:

my_project_root/views/templates/humans_list.php

<!doctype html> <html>     <head>         <meta charset="utf-8">         <title>humans list</title>     </head>     <body>         <h1>humans list</h1>         <ul>             <?php foreach($humans $human) { ?>                 <li><?php echo $human ?></li>             <?php } ?>         </ul>     </body> </html> 

my_project_root/views/humanview.php

<?php  /**  * class representing view  */ class humanview {     /** @var array contains list of names */     var $humans;      /**      * renders view in html      * @return void      */     public function render()     {         // proccess names follow constraints         $this->process($this->humans);          // here want use template in view,         // need access "$humans" variable         require 'templates/humans_list.php';     }      /**      * proccess humans names cut surnames long names      * @param  array &$names      * @return void      */     private function process(&$names)     {         foreach ($names $key => $name)         {             if (strlen($name) > 15)             {                 $name_parts = explode(' ', $name);                 if (isset($name_parts[1])) {                     $name_parts[1] = strtoupper(substr($name_parts[1], 0, 1)) . '.';                     $names[$key] = implode(' ', $name_parts);                 }                 else {                     $names[$key] = substr($name, 0, 15);                 }             }         }     }      // rest of code     // ... }  // in controller i'll use view , set names // retrieved model, make testing easier // set object here , set mock array test // render method, doesn't recognize $humans variable $view = new humanview(); $view->humans = [     'john first smith',     'peter second johnson',     'marcus third willians',     'lucas fourth brown' ]; $view->render(); 

i expected render method printed html template name in $human variable unordered list (in ul tag). said before, variable not recognized, tough set it.

an html more or less this:

humans list

  • john f. smith
  • peter s. johnson
  • marcus t. willians
  • lucas f. brown

but this:

human list

not single name being printed.

what problem in code doesn't let template render variable set?

suppose template file should reference object, since far can see $humans variable won't automagically jump class variable method that. try this:

<?php foreach($this->humans $human) { ?>     <li><?php echo $human ?></li> <?php } ?> 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -