|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
eierkoek Smarty n00b
Joined: 04 Nov 2007 Posts: 2
|
Posted: Sun Nov 04, 2007 3:21 pm Post subject: Removing newlines |
|
|
I had a template file with the following content:
Code: | {get_username assign='username'}
{if $username != 'admin'}
{show_404}
{else}
{redirect_inside url='messages/'}
{/if} |
This compiled to content with newlines (because each template-function is on a new line). PHP gave a warning because redirect wanted to send a header, and a few newlines where allready send.
I could have rewritten every template to remove the newlines between template-functions, but that would have made my templates harder to read:
Code: | {get_username assign='username'}{if $username != 'admin'}{show_404}{else}{redirect_inside url='messages/'}{/if} |
These are just examples, but to solve this problem without rewriting the templates I created the following precompile-filter:
Code: | <?php
function PreCompileFilter_RemoveSmartyNewline($templateSource, &$smarty) {
return str_replace(Array($smarty->right_delimiter."\r\n".$smarty->left_delimiter, $smarty->right_delimiter."\n".$smarty->left_delimiter), Array($smarty->right_delimiter.$smarty->left_delimiter, $smarty->right_delimiter.$smarty->left_delimiter), $templateSource);
}
?> |
and created smarty with this precompile filter enabled:
Code: | include('PreCompileFilter_RemoveSmartyNewline.php');
$smarty->register_prefilter('PreCompileFilter_RemoveSmartyNewline'); |
-------------------------------------------------------------------
A different solution would have been to {strip} every template file automatically so no newlines would have been in the output. This can also be done with a precompile filter:
Code: | <?php
function PreCompileFilter_Strip($templateSource, &$smarty) {
return $smarty->left_delimiter.'strip'.$smarty->right_delimiter.$templateSource.$smarty->left_delimiter.'/strip'.$smarty->right_delimiter;
}
?> |
This will make every template stripped of newlines (and more). So the output will be one long <html><..........></html> string. Nice and compact, but harder to debug.
Greats, Eierkoek. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Nov 04, 2007 5:20 pm Post subject: Re: Removing newlines |
|
|
eierkoek wrote: | I had a template file with the following content:
Code: | {get_username assign='username'}
{if $username != 'admin'}
{show_404}
{else}
{redirect_inside url='messages/'}
{/if} |
|
This logic does not belong in the templates. Do this decision in PHP to either display('404.tpl'), or http redirect. |
|
Back to top |
|
eierkoek Smarty n00b
Joined: 04 Nov 2007 Posts: 2
|
Posted: Sun Nov 04, 2007 7:56 pm Post subject: |
|
|
Our framework (using smarty) works a little different with smarty how it is 'suppose to be used'.
Our model is giving function that can be used in tempates... The template files decide how and what is displayed (so not php).
Creating the site for normal HTML4.0 users and for mobile phone users will give no change in the model, just in the template files.
In case of a messagebox-system:
It is not PHP that decides how many messages are shown your inbox, neither some variable in the URL. It is the template that decides how many messags are shown. So rewriting the page for mobile-phone users will only be a change in the template files and nothing in PHP. In other words: the template requests information of the model, and the model is not pushing information to the template.
Notice: Template files does 'not' handle security related stuff as my previous example may have looked like.
My example (maybe poorly choosen) was just an example how to remove the newlines in the output in html when using multiple taglines after eachother in a template-file.
I hope this will make things clear why I needed the precompiler pugin. |
|
Back to top |
|
|
|
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
|