Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
register_block: blocks with multiple content areas

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  

Is this feature useful
Yes
50%
 50%  [ 2 ]
No
50%
 50%  [ 2 ]
Total Votes : 4

Author Message
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 72

PostPosted: Tue Nov 03, 2009 6:35 am    Post subject: register_block: blocks with multiple content areas Reply with quote

Let's consider the following code that outputs address line:
Code:
{$zipcode}, {$country}, {$state}, {$city}, {$street}, {$apartment}

If any of the variables (for example $state variable) is empty, it will output something like this:
Code:
1111, UK, , London, Street 1, App. 2

There will be two adjucent commas (", ,"). To solve this problem, I suggest to extend register_block function, so that it can be used to define blocks with multiple content areas:
Code:
{blockname}...{next}...{next}...{/blockname}

This new syntax could be used in the following way:
Code:
{address separator=', '}{$zipcode}{next}{$country}{next}{$state}{next}{$city}{next}{$street}{next}{$apartment}{/address}

Code:
$smarty->register_block('address', 'address');

function address($args, $content_array, &$smarty, &$repeat)
{
  foreach ($content_array as $i => $x) if (trim($x) === '') unset($content_array[$i]);
  return implode($args['separator'], $content_array);
}
Back to top
View user's profile Send private message
aboehlke
Smarty n00b


Joined: 30 Aug 2010
Posts: 2

PostPosted: Mon Aug 30, 2010 10:51 am    Post subject: Reply with quote

This can be done with $smarty->_tag_stack. Contact me, if interested.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Aug 30, 2010 1:50 pm    Post subject: Reply with quote

This can already be done with {if} statements, I don't see how this is any better. You have to learn a new syntax for a small thing. You could also assign your values as an array to the template, and do {$address_info|implode:","}
Back to top
View user's profile Send private message Visit poster's website
aboehlke
Smarty n00b


Joined: 30 Aug 2010
Posts: 2

PostPosted: Tue Aug 31, 2010 8:27 am    Post subject: Reply with quote

How can you did this with if statements?

I did it with block functions:

Code:


function do_join ($params, $content, &$smarty, &$repeat)
{
    if (isset($content)) {
        return implode($params['separator'], $smarty->_tag_stack[count($smarty->_tag_stack) - 1]['elements']);
    }
    else {
        $smarty->_tag_stack[count($smarty->_tag_stack) - 1]['elements'] = array();
    }
}

function element($params, $content, &$smarty, &$repeat)
{
    if(trim($content)) {
        $smarty->_tag_stack[count($smarty->_tag_stack) - 2]['elements'][] = $content;
    }
}



Register .... join and element

and

Code:


{join separator=","}
    {element}{$part1_possibly_empty}{/element}
    {element}{$part2_possibly_empty}{/element}
    {element}{$part3_possibly_empty}{/element}
{/join}

[/code]
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Aug 31, 2010 1:46 pm    Post subject: Reply with quote

another method:

Code:
{capture assign=addr}{$zipcode},{$country},{$state},{$city},{$street},{$apartment}{/capture}
{$addr|str_replace:",,":","}
Back to top
View user's profile Send private message Visit poster's website
olivierpons
Smarty Rookie


Joined: 13 Aug 2011
Posts: 13
Location: France

PostPosted: Sat Aug 13, 2011 9:37 am    Post subject: Reply with quote

mohrt wrote:
another method:

Code:
{capture assign=addr}{$zipcode},{$country},{$state},{$city},{$street},{$apartment}{/capture}
{$addr|str_replace:",,":","}


This may work, but this is a workaround (thanks for this), but not a clean stuff for designers.

I had almost the same request.
I sometimes have a lot of things to test.
Code:
<input type="text" id="annoncetitre" name="annoncetitre" size="35" maxlength="50" {if isset($tab_err.annoncetitre)}onKeyup="myfunc();"{/if} {if isset($tab_post_ok.annoncetitre)}value="{$tab_post_ok.annoncetitre}"{else}value=""{/if} />

It's not readable if it's on only one line, so I explode the stuff in 2-3 lines like this:

Code:
<input type="text" id="annoncetitre" name="annoncetitre" size="35" maxlength="50"
  {if isset($tab_err.annoncetitre)}
    onKeyup="myfunc();"
  {/if}
  {if isset($tab_post_ok.annoncetitre)}
    value="{$tab_post_ok.annoncetitre}"
  {else}
    value=""
  {/if}
 />


Thus the generated code looks like this:
Code:
<input type="text" id="annoncetitre" name="annoncetitre" size="35" maxlength="50"
    onKeyup="myfunc();"
    value=""
 />


And it would be so damn cool to have something like:

Code:
{join separator=" "}
  <input type="text" id="annoncetitre" name="annoncetitre" size="35" maxlength="50"
    {if isset($tab_err.annoncetitre)}
      onKeyup="myfunc();"
    {/if}
    {if isset($tab_post_ok.annoncetitre)}
      value="{$tab_post_ok.annoncetitre}"
    {else}
      value=""
    {/if}
   />
{/join}


Then the resulting code would be perfect:
Code:
<input type="text" id="annoncetitre" name="annoncetitre" size="35" maxlength="50" onKeyup="myfunc();" value="" />
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


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

PostPosted: Sat Aug 13, 2011 10:10 am    Post subject: Reply with quote

Code:
<div
   id="foo"
   class="bar"
   style="width:100px"
>foo</div>
is perfectly legal (read: valid) HTML.

If you're serious about WPO, you should consider gzipping your HTML. This will compress the code much better than merely removing pointless whitespace.

Anyways, Smarty know the {strip} block. It will reduce any whitespace and line breaks. It is, however, a bit overzealous.
Code:
{strip}<div
   id="foo"
   class="bar"
   style="width:100px"
>foo</div>{/strip}
will result in
Code:
<divid="foo"class="bar"style="width:100px">foo</div>


You're left with multiple options. Have a look at the trimWhitespace outputfilter . Or register your own strip block, that could look somewhat like
Code:
function block_strip_nl(array $params, $content, $template, &$repeat) {
  $content = preg_replace( '#>[\s\n\r\t]+<#uS', '><', $content );
  $content = preg_replace( '#[\s\n\r\t]{2,}#uS', ' ', $content );
  return $content;
}


this basically kills all white space between tags and reduces duplicate white space to a single whitespace.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
olivierpons
Smarty Rookie


Joined: 13 Aug 2011
Posts: 13
Location: France

PostPosted: Sat Aug 13, 2011 11:19 am    Post subject: Reply with quote

Thank you very much for your answer and suggestion.

The code:
Code:

<div
   id="foo"
   class="bar"
   style="width:100px"
>
foo
</div>

is valid.
But

Code:
<div id="foo" class="bar" style="width:100px">foo</div>

is 11 bytes less. So coming from 66 bytes to 55 bytes.
It's a gain of 17%.
So if a "not optimized" page takes 10 seconds to load on a iPhone, it would take ~2 seconds less for my own page.
This is important for me. This is a quality criteria.

Thank you very much indeed again.

Now with your code, and the {strip}{/strip} I came from 5775 bytes to 5009, which is a 15% gain Laughing

Very happy. And it feels like it's real professionnal work. Wink
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


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

PostPosted: Sat Aug 13, 2011 11:53 am    Post subject: Reply with quote

Here's a little test for you: https://gist.github.com/1143770

Results:

The original file (Apple's home page) is 9632 bytes in size. Running it through the trimwhitespace output filter (eliminating duplicate white space, removing comments, etc) leaves 8759 bytes. Removing spaces and comments on this particular page shaves off 873 bytes (deflates to 90%).

Running the original file through gzip reduces the size to 3129 bytes, thus removing 6503 bytes (deflates to 32%).

Running the trimmed file through gzip reduces the size to 2832 bytes, getting down to 29% of the initial file size (while maintaining the 32% trimmed -> gzipped).

I hate to break it to you, but the difference of 873 bytes achieved by sole trimming is no noteworthy accomplishment. The removal of 6503 bytes (~6KB) on the other hand IS a noteworthy improvement. Seeing that the trimmed gzip result is only about 300 bytes less, I'd save myself the hassle of wasting CPU and potentially introducing problems because of the removed comments / whitespace.

Read up on mod_deflate - or whatever comparable module your webserver of choice might offer.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
olivierpons
Smarty Rookie


Joined: 13 Aug 2011
Posts: 13
Location: France

PostPosted: Sat Aug 13, 2011 12:20 pm    Post subject: Reply with quote

You are perfectly right.
This may just be a commercial's point of view: if you show that your code is 100% optimized to a non-informatician, he'll understand when you go: "show source => everything is concatenated.".

On the contrary, trying to explain what zipped means, show the firebug console and so on may be... let's say "more complicated" for him/her. Neutral

{strip}{/strip} could have been called

{optimize_to_help_me_sell_my_site}
{/optimize_to_help_me_sell_my_site}

...
sorry bad joke. Very Happy
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests 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