Thursday, December 19, 2013

"Sinking" output

Bedrock is a templating language which means pretty much everything in your page is being output to the browser. Bedrock statements that are parsed and produce output, do so at the point where the tag has been placed. Makes sense...after all that's what a templating engine is supposed to do.

 Hi this is <var $name> from <var $company_name>.

So when you have a page that includes statements like:

<sqlselect "select * from customer where name like ?" --bind=$input.q>
  <li><var $first_name> <var $last_name></li>
</ul> the middle of your page, you might be surprised to see a lot of white space in the output.

That's because, while Bedrock interprets the tags and does not output anything as a result of non-output tags other than <var>, the Bedrock statements are on lines that include a new line and Bedrock dutifully outputs anything in your page that is not a tag.

One way to avoid this, is to avoid placing these tags on their own line.  Another way of eating white space is to use the sink tag.  Take this group of statements:

<null:foo $>
<if $foo --eq "bar">
  <null:baz "buz">

Bedrock will interpret these tags, but your output will contain 4 new lines.  Using the <sink> tag you can eat all of that white space.

<sink><null:foo $>
<if $foo --eq "bar">
  <null:baz "buz">

The <sink> tag can scoop up the contents of the output in a variable as well.

<sink:hello_world>Hello World!</sink>

<sink> has one other magical property. It can also output the contents of the block to a handle.

<sink $fh>Hello World!</sink>

You can even nest <sink> tags.

<sink:foo>bar<sink>this is a comment</sink></sink>

Check out this link for more detail about the sink tag.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.