Tags:
create new tag
, view all tags

Avoiding Run-on Lines in Templates

To avoid untoward interactions in template expansion, many templates are written as (sets of) run-on lines. IMNSHO, this makes them difficult to understand, edit, etc. Some of these run-on lines are simply unnecessary, because the content of the template is of a type (e.g., CSS, JavaScript) that can handle embedded newlines. Others can be eliminated with a bit of extra effort.

Line-folding may be allowed.

Line-folding is allowed in CSS. In addition, CSS declarations are supposed to be made in the HEAD portion of a web page, where extra white space should not be an issue. So, if line breaks in CSS are causing pages to render differently, this indicates a problem in the template architecture, etc.

JavaScript also allows line folding, but the code is expanded into the BODY portion of pages. The best solution is to put the JavaScript into separate files (e.g., "foo.js") which are not processed by the template mechanism. However, inline JavaScript code can also be folded, with a bit of care.

In the example below, no white space is allowed outside of the "script" tags. So, there should be no effect on the rendered page:

%TMPL:DEF{"local_js"}%<script type="text/javascript">
    var img_hide, img_show;

    img_hide =  '%LOCALSKIN%/hide.gif';
    img_show =  '%LOCALSKIN%/show.gif';
</script>%TMPL:END% 

If line-folding is disallowed...

Most templates contain HTML, so embedded white space (including newlines) may show up in undesirable locations. One workaround is to use temporary variables to encode snippets of template text:

%TMPL:DEF{"abc_1"}%...%TMPL:END%
%TMPL:DEF{"abc_2"}%...%TMPL:END%
%TMPL:DEF{"abc"}%%TMPL:P{"abc_1"}%%TMPL:P{"abc_2"}%%TMPL:END%

Dividing the template into a number of (relatively) short chunks allows each one to be edited independently. If the context where a chunk is to be expanded does not allow extra white space, it is very simple to make sure that none is used.

There are, to be sure, some annoying issues with this approach:

  • Each variable use requires a dozen overhead characters.

  • Template variables live in a global namespace, so naming is an issue.

  • Template variable expansion requires processing resources and delays rendering.

However, this is the best hack I've found so far...

-- Contributors: RichMorin - 20 Nov 2006

Discussion

Yes, for ease of maintenance, it would be nice to use more whitesapce. The main reason for avoiding empty lines is that TWiki converts that into <p /> tags, which can introduce unwanted results, such as paragraph tags between a table tag and a table header tag.

-- PeterThoeny - 22 Nov 2006

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2006-11-22 - PeterThoeny
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.