Timo.Klement
Timo.Klement

Reputation: 637

Can Smarty read only one block instead of in template?

For the beginning I'm really new to Smarty. I got some template files that need to be parsed sometimes first. So it would be interesting to know if Smarty can parse only one special block instead of the whole file with many blocks. There is a function called fetch () but it doesn't work that way I hoped.

It would be nice if it is possible to do something like

Smarty()->fetch($myTpl, array('blockname1'));

Upvotes: 3

Views: 757

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111869

As far as I know you cannot do it, but you can have multiple Smarty files. So what you can do:

One Smarty file (for example index.tpl):

bla bla bla 
{$content}
bla bla bla

Another Smarty file (for example site.tpl)

another bla bla bla 

In PHP you can now do:

$site = $smarty->fetch('site.tpl');
$smarty->assign('content', $site . ' xxx'); // you modify in PHP content a bit
$smarty->display('index.tpl');

And the output will be:

bla bla bla 
another bla bla bla xxx
bla bla bla

So as you see you need to put block you want to parse separately into another file, then fetch it and then you can assign its content to another Smarty file (or do anything you want with it - save to file and so on).

Upvotes: 1

Related Questions