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

Removing newlines

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
eierkoek
Smarty n00b


Joined: 04 Nov 2007
Posts: 2

PostPosted: Sun Nov 04, 2007 3:21 pm    Post subject: Removing newlines Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sun Nov 04, 2007 5:20 pm    Post subject: Re: Removing newlines Reply with quote

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
View user's profile Send private message Visit poster's website
eierkoek
Smarty n00b


Joined: 04 Nov 2007
Posts: 2

PostPosted: Sun Nov 04, 2007 7:56 pm    Post subject: Reply with quote

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
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 -> 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