Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:21:20+00:00 2026-06-06T22:21:20+00:00

I am working on templating in PHP. I know there are templating engines out

  • 0

I am working on templating in PHP. I know there are templating engines out there but i want something simple for a small project.
Here is my problem:

Most of my output are stored in a variable $contents and assigned to the template to display in the content section of my page

But sometimes i have echo(“blah blah”) in my Model [like in case of exception, query failure] and i want to pass that to my page.

problem is this output shows before the page e.g.

$contents = "I want to show this";

$news = $news->getNews();

//concatenate
$contents = $contents.''.[resultsfromnews]

$template = new Template();
$template->content = $contents; //and so on
$template->display();

if $news->getNews() method or the methods it calls echoes something e.g. (‘database specific error, exception), they show before the $template->display() is called

Custom error information/notification from getNews() showing here before page content

Normal Page Content

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse
molestie consequat, vel illum dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id

Please help me with a solution… how those output could be part of my content to display.

I feel this issue could occur with all the popular template engines out there (Smarty, Twig, phpsavant etc) – how was it taken care of?

*I have been searching for awhile to know how it works in other template engines – have not got close *

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:21:21+00:00Added an answer on June 6, 2026 at 10:21 pm

    To answer your question:

    $contents = "I want to show this";    
    
    ob_start();
    $news = $news->getNews();
    $buffer = ob_get_contents();
    ob_end_clean();
    
    //concatenate
    $contents = $contents.$buffer.''.[resultsfromnews]
    
    $template = new Template();
    $template->content = $contents; //and so on
    $template->display();
    

    However, one should not be echoing within models. It is better to throw exceptions and catch them just before setting the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small project, where I'm using the codeigniter php framework, but
I've been working with PHP code that generates HTML without any templating, and it's
I'm using T4 templating to generate some .config files in a project I'm working
I'm working on a project that uses the velocity templating system, so the $
I'm working on a small templating engine, and I'm using DOMDocument to parse the
I'm working on a website that uses PHP (with Savant templating for MVC) on
I'm currently working on a PHP project using CodeIgniter as my framework. I took
I'm working on a simple templating system. Basically I'm setting it up such that
I am working on a project internally, and want to use simplemodal to display
I am working on templating system. I do have input of html document from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.