Jump to content

Template Parsing Help


christophermichael

Recommended Posts

I am about as new to PHP as one can get but I had done something similar to what I am trying to do now nearly a decade ago.  I am using perhaps the most simple templating system I can.  I have VERY specific needs and 'engines' such as SMARTY are too big for my purposes.  My program is quite simple as far as the templates go. Straight HTML-based template file with tags.  Naturally.  I will have a specific list of usable tags defined for use by the end-user to create their templates.

 

I will post an example of the template class file, the html template file and one of the php files that will tie it together.

 

I want to state that everything is working exactly as it should except for template tags which invoke a function.  The functions are running exactly as they should and they output exactly what they should. However what it puts out is being put before the opening HTML tag.  I haven't shown the code yet but the value of [[site_title]] DOES show between the HTML title tags as it should.  But the two which kick off functions are before the HTML.

 

I ran into the same thing when I first tried a very similar templating deal in PERL and someone pointed out that I needed to make everything that the functions put out concatenates to $template so that it is 'compiled' within the template.  But I am not sure if this is what I need to do in PHP or exactly how I would. It could be quite similar but unfortunately I can't recall how I had done it in PERL oh so long ago.

 

Again, I want to throw out that the IF statements within the functions do exactly what they are supposed to do.  It's just not showing where it should.

 

Any and all help is greatly appreciated.  I will be using this tiny 'engine' in several projects and I will include credit in associated files for any person(s) that are able to help.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

The description of your problem sounds familiar.  For instance:

function getTitle() {
  echo 'Title of Page';
}

echo '<TITLE>' . getTitle() . '</TITLE>';

That code will exhibit the symptoms you described.  The function is outputting the text during the call, which happens before the TITLE tag is output. 

 

The correct way to write that is:

function getTitle() {
  return 'Title of Page';
}

echo '<TITLE>' . getTitle() . '</TITLE>';

Here the function is returning the value, so it ends up in the string between the TITLE tags.

 

I think if you check your functions you will probably find this scenario.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.