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

How to include an external file?

 
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
webgen
Smarty n00b


Joined: 06 Mar 2012
Posts: 3

PostPosted: Sun Mar 11, 2012 9:32 am    Post subject: How to include an external file? Reply with quote

Hi,

I'm using Smarty for my website and I would like to include into my webpage an external "header.php" file.

For example, I've a "/mytemplate/general.php" file from my website number 1 and I would like to include inside an external file located here "http://www.website2.com/header.php".

I tried different things like the use of the {include} tag but it doesn't work.

Can you help me please?
Thank you.

Tom
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Mar 11, 2012 11:42 am    Post subject: Reply with quote

PHP code should not be included directly in templates and correspondig functionallity has been derepated and removed from the standard Smarty 3 class.

Write a plugin http://www.smarty.net/docs/en/plugins.functions.tpl or execute the PHP code in your script and assign the result to a template variable.
Back to top
View user's profile Send private message
webgen
Smarty n00b


Joined: 06 Mar 2012
Posts: 3

PostPosted: Sun Mar 11, 2012 10:19 pm    Post subject: Reply with quote

I've no idea about how to do that. Is it hard to do, could you give me an example with the informations I gave you?

Thanks again.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Mar 13, 2012 11:33 am    Post subject: Reply with quote

you could use output buffering in the php code like:

Code:

ob_start();
file_get_contents('http://www.website2.com/header.php');
$header = ob_get_clean();

$smarty->assign('header', $header);


Or you could put that code in a plugin, if you want.

http://www.smarty.net/docs/en/plugins.functions.tpl


See:
http://php.net/manual/en/function.ob-get-clean.php

http://php.net/manual/en/function.file-get-contents.php

note from that page:

"A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide."


In general I don't like to get content like this from another site in this way though, as it slows your site down and means that if one site goes down, it messes up another site. If you do it, it would be a good idea to cache the content for a long time. But, your call.
Back to top
View user's profile Send private message
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