Page 1 of 1

3 quick tips for using Smarty to improve your site.

Posted: Fri Jun 03, 2011 11:38 pm
by psy
Tip 1: Compressing CSS on the fly
I'm loving the ScriptDeploy module however sometimes it's overkill. Now that the {cms_stylesheet} tag supports Smarty, put:

[[strip]] at the start and [[/strip]] at the end of your CSS file and guaranteed, your page will load more quickly!

Tip 2: My friend the Smarty replace modifier
Often in module templates things such as input field sizes, return pages, etc are dictated by the module action. Rather than change the action php code, I use, eg:

{$input_fieldname|replace:'size="80"':'size="40"'}

Returns the result I need without resorting to javascript, messing with CSS or tampering with the original module php files.

Tip 3: Using Smarty to 'test' for javascript
I had a page that was rather large and used jQuery UI tabs in the content. Problem was that for browsers that supported javascript, there was a visible jump between the page load and the $(document).ready stuff when the tabs kicked in.

Part of the solution was to add the 'accessibility' class to the content container div and remove it with jQuery when the page was ready. Worked well, the page literally flashed onto the screen already formatted. Problem was that browsers that did not support javascript would simply see the background. Solution was to add:

Code: Select all

{capture assign=accessibility}<__script__ type="text/javascript">var x = ' class="accessibility";return x;'{/capture}
in the page head section. My jQuery script is at the bottom of the page so no reference to it here.

In the content area:

Code: Select all

<div id="pagewrapper" {$accessibility}>
and in my jQuery statement:

Code: Select all

$(document).ready(function(){
  $('#pagewrapper').removeClass('accessibility');
});
Now only browsers that support js will have the beautiful tabs, those that don't only see the pure HTML page.


HTH,
Cheers
psy

Re: 3 quick tips for using Smarty to improve your site.

Posted: Sat Jun 04, 2011 9:50 am
by manuel
thx for the replace modifier tip! can't wait to try it!

Greetings,
Manuel

Re: 3 quick tips for using Smarty to improve your site.

Posted: Sat Jun 04, 2011 11:15 am
by psy
My plez Manuel, it's certainly saved me a lot of time and heartache.

;) psy

Re: 3 quick tips for using Smarty to improve your site.

Posted: Wed Jun 08, 2011 10:52 am
by lucy
Hey seems to be very good tipps. Re Tip2: where do you put this line? In the Module Template? I didn't get it to work yet...

Re: 3 quick tips for using Smarty to improve your site.

Posted: Wed Jun 08, 2011 11:07 am
by uniqu3

Re: 3 quick tips for using Smarty to improve your site.

Posted: Wed Jun 08, 2011 10:47 pm
by psy
Hi lucy

The replace modifier (or any smarty modifier) can be used on any tag in your templates.

The replace modifier will replace the tag's default output with whatever you choose.

Firstly, use the tag as normal and output the page, eg {mytag}. Next, look at the html code and find the text string to replace. Copy/paste that in the first option and your new text in the second option, eg:

{mytag|replace:'put the original string here':'put your replacement text here'}

As uniqu3 suggests, the best place to start is to read the smarty documentation.

cheers
psy

Re: 3 quick tips for using Smarty to improve your site.

Posted: Tue Jul 26, 2011 8:10 pm
by FletcherT
Oh man, the replace modifier suggestion, thanks!

-Fletcher

Re: 3 quick tips for using Smarty to improve your site.

Posted: Sun Aug 07, 2011 8:49 am
by dwave
The replace modifier is also an uncomplicated way to force submits of forms always to use SSL.

Just put this in your form template instead of {$fb_form_start}:

Code: Select all

{$fb_form_start|replace:'http':'https'}