Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Parse a file at the end

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Boby
Smarty Rookie


Joined: 21 Jan 2006
Posts: 21

PostPosted: Mon Dec 13, 2010 3:01 pm    Post subject: Parse a file at the end Reply with quote

Hello guys,

I am working on a software that is used by thousands of users, it's called phpLD.
The problem is I can't change the way templates are fetched from the PHP files. So I am stuck with one template file for each PHP file. Yes, I know it's not the best method but I can't change that.

A template file looks similar to this:
Code:
{include file='page_header.tpl'}

// content goes here

{include file='page_footer.tpl'}

(There are way many more includes, depending on each file, so inheritance in Smarty3 is not really an option)

Now I want to make it somehow, load all Javascript that is added in most cases, in each main template file using IF..ELSE, in the page_header.tpl file via a custom class.
In other words, all Javascript & CSS that is added in each template file should be sent to the <head></head>. This should improve speed as it's loaded once and only the required libraries are loaded and not everything we have.

Is there any way, I can tell Smarty to parse page_header.tpl at the very end?
The only way I can thing now, is to extend the Smarty class and in the fetch method add a parser that detects the </head> tag and injects my code from the stack. But this is a bad practice Sad

Thank you in advance!
Boby
_________________
My Homepage: www.frozenminds.com
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Dec 13, 2010 3:39 pm    Post subject: Reply with quote

You should still be able to update your templates to use inheritance. Replace your tpl files that look like this:

Code:
{include file='page_header.tpl'}

// content goes here

{include file='page_footer.tpl'}


To look like this:

Code:
{extend file="layout.tpl"}

{block name=body}
// content goes here
{/block}


Then make a new template named layout.tpl that looks like this:

Code:
// paste header.tpl contents here, add an empty "head" block, example:
<html>
<head>
  /* common head stuff here */
  {block name=head}{/block}
</head>
<body>
// now the body block
{block name=body}{/block}
// paste footer.tpl contents here, example:
</body>
</html>


Now anytime you want to add something to the head of any template, define the "head" in the template you are loading:

Code:
{extend file="layout.tpl"}
// put custom header stuff here, example:
{block name=head} /* load js, css, etc. */ {/block}
{block name=body}
// content goes here
{/block}
Back to top
View user's profile Send private message Visit poster's website
Boby
Smarty Rookie


Joined: 21 Jan 2006
Posts: 21

PostPosted: Mon Dec 13, 2010 5:45 pm    Post subject: Reply with quote

Thank you mohrt, I will try that ASAP.

Can a template extend more than one other template file? Or is this just like extending a class in PHP where it can't. Something like:
Code:
{extend file="layout.tpl"}
{block name=body}
// body content goes here
{/block}

{extend file="header.tpl"}
{block name=body}
// header content goes here
{/block}


Boby
_________________
My Homepage: www.frozenminds.com
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Dec 13, 2010 8:48 pm    Post subject: Reply with quote

each file can only extend one template, and it must be the first line of the file. you can chain them together though. Also if you want, the template you are extending (layout) could just {include} the header/footer instead of cut/paste the code. But putting it all in layout is just cleaner.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP