PHPFront logo
PHPFront Online Documentation now available! PHPFront is available on Composer: composer require ox-harris/phpfront Contact us to place an ad here...

Rendering

Create the HTML output from the set template

The PHPFront::render() function is called to render all assigned data, imports and includes into a template. This will print a well-formed HTML output with line-breaks and indentation.

PHPFront::$allow_html_formatting can be set to false to disable line-breaks and indentation.

Function

    
      
      PHPFront::render([$print = true]);
      
    
    

This function accepts one optional parameter, $print – a Boolean true or false that tells whether to actually print the rendered template or simply return it as a string. The default is true – print the template

Usage

      
        
        // Render and print our template
        $PHPFront->render();
        
        // Render but return document as string for further use
        $document = $PHPFront->render(false);
        echo $document;
        
      
      

A similar function is PHPFront::getRendered().
This function works like PHPFront::render() except that it always returns the ouput and never prints.

      
        
        PHPFront::getRendered([$element_selector = null]);
        
      
      

The optional $element_selector parameter specifies elements to return.

      
        
        // Return menu list-items from the document for further use
        $menu_list_elements = $PHPFront->getRendered('#menu li');
        echo $menu_list_elements;
        
      
      

If the last template set has not been rendered, PHPFront::getRendered() will automatically render it before returning the elements specified, if any.

If the optional $element_selector parameter was omitted or is null, it would get the whole rendered document - just like PHPFront::render(true).

Reparse Rendering

With PHPFront, you could choose to assign data to yet another assigned data. This happens when an inserted content contains HTML markup, and you assigned data with selectors that target the elements within these markup.

To enable this mode, set the PHPFront::$parse_inserted_data property to true. PHPFront will render the template a second time finding the inserted markups and setting their assigned data. Elements rendered on the first round will be ignored.

Here is an example, with some data obtained from database:

    
      
	  // Assume that $db_result['article'] contains some markup: 
	  $db_result['article'] = 'This is an excerpt from <span data-user_info="first_name"></span>\'s article.';
      
	  $author_name = $db_result['first_name'];
      
    
    
Reparse Rendering:
    
      
      // Assign content to all span elements where the author's name should appear.
      // Target ALL the elements in the main template and those that are yet to be inserted
      $PHPFront->assign('span[data-user_info="first_name"]', $author_name);
      
      // The content of this $db_result['article'] will pick up the $author_name too
      $PHPFront->assign('#article_wrapper', $db_result['article']);
      
      // The main template
      $PHPFront->setTemplate('../../resources/template.html');
      
      // The setting
      $PHPFront->parse_inserted_data = true;
      
      // Render
      $PHPFront->render();
      
    
    
Result:
    
      
      <…>
        <div id="article_wrapper"> This is an excerpt from <span data-user_info="first_name">John Doe</span>'s article
        </div>
      </…>
      
    
    

But there is another way to do this.

Alternative:
    
    
      // 1. We set the markup as a template first
      $PHPFront->setTemplate($db_result['article']);
      
      // 2. We want the author's name to show inside that span element in our markup
      $PHPFront->assign('span[data-user_info="first_name"]', $author_name);
      
      // 3. Save our string to a variable, do not print
      $pre_rendered_article = $PHPFront->render(false);
      
      // Finally, process the main template
      
      // 1. We set the main template as a file
      $PHPFront->setTemplate('../../resources/template.html');
      
      // 2. Send our $pre_rendered_article to an element in the main template
      $PHPFront->assign('#article_wrapper', $pre_rendered_article);
      
      // 3. Render and print
      $PHPFront->render();
      
    
    

The #article_wrapper element will look like the result above. Only that this alternative method will require the same process for each propsective markup string. Where performance is critical, this alternative should be used instead of reparse-rendering.

Summary

  • The PHPFront::render() outputs a HTML string representing the set template with the assigned data, imports and includes all inserted. Recall
  • PHPFront::render() will print output by default, eliminating the need to manually echo the output. Recall
  • Reparse Rendering can set data on other inserted elements. Recall

We've covered this key API in PHPFront!

Back to Working with PHPFront

See Also