Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Use PHP source file itself as a template.

 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1136

PostPosted: Tue Feb 16, 2016 12:38 am    Post subject: Use PHP source file itself as a template. Reply with quote

Code:
$smarty->registerFilter('pre',
  function($tpl_source, Smarty_Internal_Template $template)
  {
    $stack = stristr($tpl_source, '__HALT_COMPILER');
    return $stack ? substr($stack, strpos($stack, '?>') + 2) : $tpl_source;
  }
);


Your main code file should look similar to
Code:
<?php

require_once 'smarty-config.php';

$smarty->assign(array('x' => 2, 'y' => 3));

$smarty->display(__FILE__);

__halt_compiler();
?>{$x*$y}


Very useful for quick tests and demo pieces.


Last edited by AnrDaemon on Mon Dec 05, 2016 1:42 am; edited 2 times in total
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1136

PostPosted: Mon Jul 04, 2016 11:21 pm    Post subject: Reply with quote

And now, for a fully XHTML compliant PHP Smarty embeded template source!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks 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