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

intending output nicely

 
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
hkirsman
Smarty Rookie


Joined: 13 Apr 2011
Posts: 10

PostPosted: Wed Apr 13, 2011 7:59 am    Post subject: intending output nicely Reply with quote

I have a question too but first here's what I have done.

I did a plugin named partial that is basically include plugin.

If the template is multiline you can assign indent parameter like this:
{partial tpl="nav" indent=" "}

It is the same amount of white space that is in front of the partial syntax.

So now the output is indending the right way. I need this because I'm writing open source software for making static HTML sites (prototyping purposes). Output goes to programmer so it has to look the same way I write it.

Code:

    $a_output = explode("\n", $output);
    $first_line = true;
    foreach ($a_output as $key => $var) {
        if (!$first_line || $params["fullindent"]) {
            $a_output[$key] = $params["indent"] . $var;
        } else {
            $first_line = false;
            $a_output[$key] = $var;
        }
    }
    // fix: remove lines with only spaces
    $a_outputFinal = array();
    foreach ($a_output as $key => $var) {
        if (trim($var) != "") {
            $a_outputFinal[$key] = $var;
        }
    }
    return implode("\n", $a_outputFinal);
}

?>



fullindent parameter is if I want to for some reason indent the first line too.

There was some kind of bug with whitespaces so I did the // fix part. It was so long ago that I don't evene remember what it was Smile

Anyways, now everything is good!

But is there somehow possible to find automatically how many spaces I need to indent?
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Wed Apr 13, 2011 9:28 am    Post subject: Reply with quote

The Browser does not need indentation at all. In fact, if you remove all the whitespace between tags you not only reduce the size to transfer, but also reduce the overhead of the browser's html parser. That is, it does not need to hold whitespace nodes in memory. In production environments you should remove this unnecessary data.

That being said, for development a proper indentation may help. Maybe http://de.php.net/tidy can help you. Otherwise I don't see a way around writing an output-filter that implements some sort of simple parser to build a dom and serializing it with injecting indentation. If you don't want to write your own parser, you could try DOMDocument::loadHTML.

keep in mind that this should only be done for development environments. You not only produce more data to transfer, but also waste time producing it.
Back to top
View user's profile Send private message Visit poster's website
hkirsman
Smarty Rookie


Joined: 13 Apr 2011
Posts: 10

PostPosted: Wed Apr 13, 2011 1:45 pm    Post subject: Reply with quote

Thanx for input, globe!

I do HTML prototypes that go on to developers. Maby you heard of StaticMatic? My software is much like that but it uses Smarty and php. You write some templates and build static html from it (while looking at localhost/text.html, it always shows the Smarty generated page and writes text.html to disk).

Nice indenting is for developers who need to cut it in half again and make it dynamic.

I totally agree that extra white-space is bad in web.

So for now my plugin that gets the indent level it needs as parameter is the way to go...

Tidy has it's own indenting logic and changes stuff the way I don't want. But I guess having the possibility of using Tidy would come in handy.


globe wrote:
The Browser does not need indentation at all. In fact, if you remove all the whitespace between tags you not only reduce the size to transfer, but also reduce the overhead of the browser's html parser. That is, it does not need to hold whitespace nodes in memory. In production environments you should remove this unnecessary data.

That being said, for development a proper indentation may help. Maybe http://de.php.net/tidy can help you. Otherwise I don't see a way around writing an output-filter that implements some sort of simple parser to build a dom and serializing it with injecting indentation. If you don't want to write your own parser, you could try DOMDocument::loadHTML.

keep in mind that this should only be done for development environments. You not only produce more data to transfer, but also waste time producing it.
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